Jquery Ajax Posting JSON to webservice(Jquery Ajax 将 JSON 发布到 Web 服务)
问题描述
我正在尝试将 JSON 对象发布到 asp.net 网络服务.
I am trying to post a JSON object to a asp.net webservice.
我的 json 看起来像这样:
My json looks like this:
var markers = { "markers": [
{ "position": "128.3657142857143", "markerPosition": "7" },
{ "position": "235.1944023323615", "markerPosition": "19" },
{ "position": "42.5978231292517", "markerPosition": "-3" }
]};
我正在使用 json2.js 对我的 JSON 对象进行字符串化.
I am using the json2.js to stringyfy my JSON object.
我正在使用 jquery 将其发布到我的网络服务.
and I am using jquery to post it to my webservice.
$.ajax({
type: "POST",
url: "/webservices/PodcastService.asmx/CreateMarkers",
data: markers,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){alert(data);},
failure: function(errMsg) {
alert(errMsg);
}
});
我收到以下错误:
无效的 JSON 原语
Invalid JSON primitive
我发现了一堆与此相关的帖子,这似乎是一个非常普遍的问题,但我没有尝试解决这个问题.
I have found a bunch of posts relating to this and it seems to be a really common problem but nothing i try fixes the issue.
当萤火虫发布到服务器时,它看起来像这样:
When firebug what is being posted to the server it looks like this:
markers%5B0%5D%5Bposition%5D=128.3657142857143&markers%5B0%5D%5BmarkerPosition%5D=7&markers%5B1%5D%5Bposition%5D=235.1944023323615&markers%5B1%5D%5BmarkerPosition%5D=19&markers%5B2%5D%5Bposition%5D=42.5978231292517&markers%5B2%5D%5BmarkerPosition%5D=-3
markers%5B0%5D%5Bposition%5D=128.3657142857143&markers%5B0%5D%5BmarkerPosition%5D=7&markers%5B1%5D%5Bposition%5D=235.1944023323615&markers%5B1%5D%5BmarkerPosition%5D=19&markers%5B2%5D%5Bposition%5D=42.5978231292517&markers%5B2%5D%5BmarkerPosition%5D=-3
我正在调用的网络服务函数是:
My webservice function that is being called is:
[WebMethod]
public string CreateMarkers(string markerArray)
{
return "received markers";
}
推荐答案
您提到使用 json2.js 对您的数据进行字符串化,但是 POST 的数据似乎是 URLEncoded JSON 您可能已经看过了,但是 这篇关于无效 JSON 原语的帖子 介绍了 JSON 被 URLEncoded 的原因.
You mentioned using json2.js to stringify your data, but the POSTed data appears to be URLEncoded JSON You may have already seen it, but this post about the invalid JSON primitive covers why the JSON is being URLEncoded.
我会建议反对 将原始的、手动序列化的 JSON 字符串传递到您的方法中.ASP.NET 将自动对请求的 POST 数据进行 JSON 反序列化,因此,如果您手动序列化 JSON 字符串并将其发送到 ASP.NET,您实际上最终将不得不对 JSON 序列化字符串进行 JSON 序列化.
I'd advise against passing a raw, manually-serialized JSON string into your method. ASP.NET is going to automatically JSON deserialize the request's POST data, so if you're manually serializing and sending a JSON string to ASP.NET, you'll actually end up having to JSON serialize your JSON serialized string.
我会建议更多类似的内容:
I'd suggest something more along these lines:
var markers = [{ "position": "128.3657142857143", "markerPosition": "7" },
{ "position": "235.1944023323615", "markerPosition": "19" },
{ "position": "42.5978231292517", "markerPosition": "-3" }];
$.ajax({
type: "POST",
url: "/webservices/PodcastService.asmx/CreateMarkers",
// The key needs to match your method's input parameter (case-sensitive).
data: JSON.stringify({ Markers: markers }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){alert(data);},
error: function(errMsg) {
alert(errMsg);
}
});
避免无效 JSON 原语问题的关键是向 jQuery 传递 data
参数的 JSON 字符串,而不是 JavaScript 对象,以便 jQuery 不会尝试对您的数据进行 URLEncode.
The key to avoiding the invalid JSON primitive issue is to pass jQuery a JSON string for the data
parameter, not a JavaScript object, so that jQuery doesn't attempt to URLEncode your data.
在服务器端,将您的方法的输入参数与您传入的数据的形状相匹配:
On the server-side, match your method's input parameters to the shape of the data you're passing in:
public class Marker
{
public decimal position { get; set; }
public int markerPosition { get; set; }
}
[WebMethod]
public string CreateMarkers(List<Marker> Markers)
{
return "Received " + Markers.Count + " markers.";
}
如果您愿意,也可以接受一个数组,例如 Marker[] Markers
.ASMX ScriptServices 使用的反序列化器 (JavaScriptSerializer) 非常灵活,并且会尽其所能将您的输入数据转换为您指定的服务器端类型.
You can also accept an array, like Marker[] Markers
, if you prefer. The deserializer that ASMX ScriptServices uses (JavaScriptSerializer) is pretty flexible, and will do what it can to convert your input data into the server-side type you specify.
这篇关于Jquery Ajax 将 JSON 发布到 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Jquery Ajax 将 JSON 发布到 Web 服务
基础教程推荐
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 动态更新多个选择框 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01