Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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 <a href="http://encosia.com/asmx-scriptservice-mistake-invalid-json-primitive/">this post about the invalid JSON primitive</a> covers why the JSON is being URLEncoded.</p> <p>I'd <a href="http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/">advise <em>against</em> passing a raw, manually-serialized JSON string into your method</a>. 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.</p> <p>I'd suggest something more along these lines:</p> <pre><code>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);}, failure: function(errMsg) { alert(errMsg); } }); </code></pre> <p>The key to avoiding the invalid JSON primitive issue is to pass jQuery a JSON string for the <code>data</code> parameter, not a JavaScript object, so that jQuery doesn't attempt to URLEncode your data.</p> <p>On the server-side, match your method's input parameters to the shape of the data you're passing in:</p> <pre><code>public class Marker { public decimal position { get; set; } public int markerPosition { get; set; } } [WebMethod] public string CreateMarkers(List&lt;Marker&gt; Markers) { return "Received " + Markers.Count + " markers."; } </code></pre> <p>You can also accept an array, like <code>Marker[] Markers</code>, 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload