Tôi đã gặp sự cố với mã này tôi đã dành 3 giờ đồng hồ để khám phá và cố gắng tìm câu trả lời. Như tôi đã không thành công, tôi sẽ chỉ gửi mã và yêu cầu mà loại thông số tôi nên có trên dịch vụ web của tôi để xử lý yêu cầu này:ASP.NET: Vấn đề tham số jQuery AJAX 'dữ liệu'
var args = [{ key: 'myId', value: 'myValue' }, { key: 'myOtherId', value: 'myOtherValue'}];
var dataToSend = { name: 'fooId', value: 'fooValue', args: args };
$.ajax({
type: 'POST',
url: 'fooURL',
data: dataToSend,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: OnSuccess,
error: OnError
});
Bây giờ, trong đó loại chữ ký tôi nên có để có thể để có được "dataToSend" của tôi?
Tôi đã thử:
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Foo(string name, object value, List<Args> args)
{
return "OK";
}
public class Args
{
public string key { get; set; }
public object value { get; set; }
}
và
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Foo(string name, object value, object[] args)
{
return "OK";
}
và cũng
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Foo(dataToSend dataToSend)
{
return "OK";
}
public class dataToSend
{
public string name { get; set; }
public object value { get; set; }
public List<Args> args = new List<Args>();
}
public class Args
{
public string key { get; set; }
public object value { get; set; }
}
Khá chắc chắn Sam có nó ở đây. Kiểm tra http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/ để biết thêm thông tin. Giả sử chuỗi sửa lỗi nó, tôi nghĩ nó sẽ là cú pháp đầu tiên. –