为什么 jquery 在发送到 asp.net web 方法之前不将我的数组转换为 json 字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/255216/
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
Why doesn't jquery turn my array into a json string before sending to asp.net web method?
提问by EvilSyn
So far, I've only been passing javascript strings to my web methods, which get parsed, usually as Guids. but now i have a method that accepts an IList... on the client, i build this array of objects and then attempt to pass it like:
到目前为止,我只将 javascript 字符串传递给我的 web 方法,这些方法通常作为 Guid 进行解析。但现在我有一个接受 IList 的方法......在客户端上,我构建了这个对象数组,然后尝试像这样传递它:
$.ajax({
type: 'POST',
url: 'personalization.aspx/SetPersonalization',
data: "{'backerEntries':" + backerEntries + "}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: postcardManager.SetPersonalizationComplete
});
The post:
帖子:
{'backerEntries':[object Object],[object Object],[object Object]}
The error response:
错误响应:
Invalid JSON primitive: object.
For some reason, jquery doesn't seem to convert my array into a json string? Any ideas why? I tried putting [] around the backerEntries, and {}, as well as {[]} just in sheer desperation. Am I missing something obvious here?
出于某种原因,jquery 似乎没有将我的数组转换为 json 字符串?任何想法为什么?我尝试在 backerEntries、{} 和 {[]} 周围放置 [],只是出于绝望。我在这里遗漏了一些明显的东西吗?
回答by Shog9
data: "{'backerEntries':" + backerEntries + "}",
数据:“{'backerEntries':”+ backerEntries +“}”,
..is the same as
..是相同的
data: "{'backerEntries':" + backerEntries.toString() + "}",
...which is pretty much useless. Use Duncan's suggestion if you just want to pass an encoded list of values with the name "backerEntries" in your querystring. If you want to JSON-encode the data, then get a JSON libraryand call JSON.stringify()
.
……这几乎没用。如果您只想在查询字符串中传递名称为“backerEntries”的编码值列表,请使用Duncan的建议。如果要对数据进行 JSON 编码,请获取JSON 库并调用JSON.stringify()
.
回答by Duncan
The data you are passing you are trying to pass it as a string already. If you want jQuery to transform it leave the whole thing as an object, e.g.
您传递的数据已经尝试将其作为字符串传递。如果您希望 jQuery 对其进行转换,请将整个事物作为一个对象,例如
data:{backerEntries: backerEntries }
Assuming of course backerEntries is an array. jQuery should transform this and will append it to the querystring as that is its default behaviour. Your current code is relying on default JavaScript behaviour which won't by default convert an array into its string representation.
假设当然 backerEntries 是一个数组。jQuery 应该转换它并将其附加到查询字符串中,因为这是它的默认行为。您当前的代码依赖于默认的 JavaScript 行为,默认情况下不会将数组转换为其字符串表示形式。
回答by Herb Caudill
Since you're using ASP.NET, you can use the built-in ASP.NET AJAX serialization library:
由于您使用的是 ASP.NET,您可以使用内置的 ASP.NET AJAX 序列化库:
var backerEntriesJson = Sys.Serialization.JavaScriptSerializer.serialize(backerEntries);
then pass that directly in your jQuery ajax call:
然后直接在你的 jQuery ajax 调用中传递它:
...
data: backerEntriesJson,
...
回答by Herb Caudill
This is NOT valid JSON: { 'foo': 'bar' }
这不是有效的 JSON: { 'foo': 'bar' }
Isn't, wasn't ever, never will be. JSON processors are often very forgiving, which of course is a false convenience.
不是,从来没有,永远不会。JSON 处理器通常非常宽容,这当然是一种虚假的便利。
Read the specification. A string is defined to be enclosed in double quotes, not single quotes, not smiley face characters, not pieces of metal bent at right angles, not bricks. There's no mention of single quotes, period.
阅读规范。字符串被定义为用双引号括起来,不是单引号,不是笑脸字符,不是成直角弯曲的金属片,不是砖。没有提到单引号,句号。
Now, property names are JSON strings. By definition, they MUST are enclosed in double quotes.
现在,属性名称是 JSON 字符串。根据定义,它们必须用双引号括起来。
Valid: { "foo": "bar" } valid" { "foo": 100 } valid: { "foo": true } valid: { "foo": [ "one", "two" ], "bar": false }
有效:{ "foo": "bar" } 有效" { "foo": 100 } 有效:{ "foo": true } 有效:{ "foo": [ "one", "two" ], "bar":错误的 }
see www.json.org
见 www.json.org
see www.jsonlint.com
见 www.jsonlint.com