ASP.NET: jQuery AJAX 'data' 参数问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1061884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET: jQuery AJAX 'data' param problem
提问by Andre Gallo
I've been having problems with this code I had spent the last 3 hours digging around and trying to find an answer. As I wasn't successful, I will just post the code and ask which kind of parameters I should have on my web service to handle this request:
我在使用这段代码时遇到了问题,我在过去的 3 个小时里一直在挖掘并试图找到答案。由于我没有成功,我只会发布代码并询问我应该在我的网络服务上使用哪种参数来处理此请求:
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
});
Now, which kind of signature I should have to be able to get my "dataToSend"?
现在,我应该拥有哪种签名才能获得“dataToSend”?
I've tried:
我试过了:
[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; }
}
and
和
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Foo(string name, object value, object[] args)
{
return "OK";
}
and also
并且
[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; }
}
回答by Sam
Try passing the data as a string, not an object, ie:
尝试将数据作为字符串而不是对象传递,即:
$.ajax( { ... data : '{ a: 2, b: 3 }', ... } );
The reasoning for this is that if you specify an object as data then jQuery serializes the data using query string format, whereas the server is expecting JSON format directly.
这样做的原因是,如果您将对象指定为数据,则 jQuery 使用查询字符串格式序列化数据,而服务器则直接期待 JSON 格式。
This happens despite telling jQuery to use JSON as the data type - it seems to only relate to the result, not the request payload sent to the server.
尽管告诉 jQuery 使用 JSON 作为数据类型,但仍会发生这种情况 - 它似乎只与结果有关,而不与发送到服务器的请求有效负载有关。
Everything else you have there looks correct to me.
你在那里的其他一切在我看来都是正确的。
回答by Fermis
Although this is an older post I thought I would contribute. I have been sending an associative array same idea an the accepted post I just find it easier to write.
虽然这是一篇较旧的帖子,但我认为我会有所贡献。我一直在发送一个与已接受的帖子相同的关联数组,我发现它更容易编写。
Javascript
Javascript
postData[0] = 'data!';
postData[1] = 'moar data!';
postData[2] = 'and some data';
$.ajax({
type: 'POST',
url: 'postUrl',
data: { data: postData },
});
PHP
PHP
$data = $_POST['data'][0];
$moar_data = $_POST['data'][1];
$and_some_data = $_POST['data'][2];
回答by wtaniguchi
If you're working with JSON-enabled .NET WebServices/WebMethods... my tips are:
如果您正在使用支持 JSON 的 .NET WebServices/WebMethods...我的提示是:
Be very careful with web.config configuration. Use it to enable big parameters, POST method and JSON.
Use a framework to handle Object serialization e deserialization. I would recommend NewtonSoft's Json.NET.
对 web.config 配置要非常小心。使用它来启用大参数、POST 方法和 JSON。
使用框架来处理对象序列化和反序列化。我会推荐 NewtonSoft 的 Json.NET。
I don't think ASP.NET automagically do it for you, your parameters are always strings. You should take that strings, deserialize it and turn it to an array of objects.
我不认为 ASP.NET 会自动为你做这件事,你的参数总是字符串。您应该获取该字符串,对其进行反序列化并将其转换为对象数组。