And*_*llo 16 asp.net ajax jquery
我一直遇到这个代码的问题,我花了最近3个小时来挖掘并试图找到答案.由于我没有成功,我将发布代码并询问我应该在我的Web服务上使用哪种参数来处理此请求:
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
});
Run Code Online (Sandbox Code Playgroud)
现在,我应该获得哪种签名才能获得"dataToSend"?
我试过了:
[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; }
}
Run Code Online (Sandbox Code Playgroud)
和
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Foo(string name, object value, object[] args)
{
return "OK";
}
Run Code Online (Sandbox Code Playgroud)
并且
[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; }
}
Run Code Online (Sandbox Code Playgroud)
Sam*_*Sam 22
尝试将数据作为字符串传递,而不是对象,即:
$.ajax( {
...
data : '{ a: 2, b: 3 }',
...
} );
这样做的原因是,如果您将对象指定为数据,则jQuery使用查询字符串格式序列化数据,而服务器则直接期望JSON格式.
尽管告诉jQuery使用JSON作为数据类型,但这似乎只与结果有关,而不是与发送到服务器的请求有效负载有关.
你在那里的其他一切对我来说都是正确的.