在服务器端,将方法的输入参数与传入的数据的形状相匹配:公共类标记{公共小数位{获取;放;}公共 int 标记位置 { 获取;放;}}[网络方法]公共字符串 CreateMarkers(List Markers){返回收到"+ Markers.Count + "标记.";}如果您愿意,您也可以接受一个数组,例如 Marker[] Markers.ASMX ScriptServices 使用的解串器 (JavaScriptSerializer) 非常灵活,并且会尽其所能将您的输入数据转换为您指定的服务器端类型.I am trying to post a JSON object to a asp.net webservice.My json looks like this:var markers = { "markers": [ { "position": "128.3657142857143", "markerPosition": "7" }, { "position": "235.1944023323615", "markerPosition": "19" }, { "position": "42.5978231292517", "markerPosition": "-3" }]};I am using the json2.js to stringyfy my JSON object.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); } });I am getting the following error: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:My webservice function that is being called is:[WebMethod]public string CreateMarkers(string markerArray){ return "received markers";} 解决方案 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.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); }});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.";}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 发布到网络服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 06:45
查看更多