Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery/json read parameters from POST in .NET web service
    text
    copied!<p>I'm having trouble figuring out how to read a json string from a POST coming from jQuery(most articles I've seen show how to send the json, but not how to get it from the POST message received by the web services.</p> <p>This is the code I've written so far.</p> <p>On the client side:</p> <pre><code>&lt;input type="text" id="UserNameText" /&gt; &lt;br /&gt; &lt;input type="password" id="PasswordText" /&gt; &lt;br /&gt; &lt;input type="button" id="Login" value="Login" onclick="DoLogin();" /&gt; &lt;script type="text/javascript" language="javascript"&gt; function DoLogin() { var un = document.getElementById('UserNameText').value; var pw = document.getElementById('PasswordText').value; var info = "{ 'UserName':'" + un + "', 'Password':'" + pw + "'}"; $.ajax( { type: "POST", url: "http://localhost:60876/Login.asmx/LoginSpecial", dataType: 'json', data: info, contentType: "application/json; charset=utf-8", success: function (msg) { alert(msg.d); }, error: function (msg) { alert(msg.responseText); } }); } &lt;/script&gt; </code></pre> <p>on the server side I've got this:</p> <pre><code>[ScriptMethod(ResponseFormat = ResponseFormat.Json)] [WebMethod] public string LoginSpecial() { // none of these seem to be working NameValueCollection parameters = HttpContext.Current.Request.Params; string p = parameters["QUERY_STRING"] != null ? parameters["QUERY_STRING"].ToString() : String.Empty; string info = HttpContext.Current.Request["info"] != null ? HttpContext.Current.Request["info"].ToString() : String.Empty; string data = HttpContext.Current.Request["data"] != null ? HttpContext.Current.Request["data"].ToString() : String.Empty; // test json string, need to read from the jquery post string json = "{ 'UserName':'test', 'Password':'test'}"; // the following two lines of code work ok with the test json string above. JavaScriptSerializer serial = new JavaScriptSerializer(); Credentials credential = (Credentials)serial.Deserialize(json, typeof(Credentials)); return "Some json message here"; } </code></pre> <p>I've set break points and I'm hitting the web service as expected, I just can't figure out how to get the json string from the POST message.</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