Your operation is expecting a string for the rIds parameter, but you're passing an array to it. There are some conversions which happen automatically, but only very simple ones (such as numbers to string). Also, rScope is expecting a string, but you're passing an object to it.
There are a few things you can do. The first would be to pass the data as a string instead of as their "normal" type - that means stringifying both the RIds and rScope parameters:
var data = JSON.stringify({
Id: Id,
rIds: JSON.stringify(RIds),
rScope: JSON.stringify(rScope)
});
$.ajax({
url: "/Web/WebServices/Operation.svc/SetScope",
type: "POST",
contentType: "application/json; charset=utf-8",
beforeSend: function () { },
dataType: "json",
cache: false,
data: data,
success: function (data) { onSuccess(data); },
error: function (data) { onError(data); }
});
Another alternative would be in line with what François Wahl mentioned, which would be make the types which would receive the data you're sending. You'll need to do that both for the rIds and for the rScope parameters:
public class StackOverflow_13575100
{
[ServiceContract]
public class Service
{
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]
public string SetScope(int rId, string rIds, string rScope)
{
return string.Format("{0} - {1} - {2}", rId, rIds, rScope);
}
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
public string SetScopeTyped(int rId, int[] rIds, ScopeClass[] rScope)
{
return string.Format("{0} - {1} - {2}",
rId,
"[" + string.Join(", ", rIds) + "]",
"[" + string.Join(", ", rScope.Select(s => s.ToString())) + "]");
}
}
[DataContract]
public class ScopeClass
{
[DataMember(Name = "id")]
public int Id { get; set; }
[DataMember(Name = "type")]
public string Type { get; set; }
public override string ToString()
{
return string.Format("Scope[Id={0},Type={1}]", Id, Type);
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
host.Open();
Console.WriteLine("Host opened");
WebClient c = new WebClient();
c.Headers[HttpRequestHeader.ContentType] = "application/json";
string data = @"{""Id"":1,""rIds"":[1,2,3,4],""rScope"":[{""id"":3,""type"":""barney""},{""id"":2,""type"":""ted""}]}";
Console.WriteLine(data);
try
{
Console.WriteLine(c.UploadString(baseAddress + "/SetScope", data));
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
c.Headers[HttpRequestHeader.ContentType] = "application/json";
Console.WriteLine(c.UploadString(baseAddress + "/SetScopeTyped", data));
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}