JQuery / json read parameters from POST in .NET .NET service

I find it difficult to understand how to read json string from POST coming from jQuery (most of the articles I saw show how to send json, but not how to get it from POST message received on the Internet service.

This is the code that I have written so far.

On the client side:

<input type="text" id="UserNameText" />
<br />
<input type="password" id="PasswordText" />
<br />
<input type="button" id="Login" value="Login" onclick="DoLogin();" />

<script type="text/javascript" language="javascript">
    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); }
        });
    }
</script>

on the server side I have this:

[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";
}

I set breakpoints and I hit the web service as expected, I just can't figure out how to get the json string from the POST message.

+3
source share
3 answers

If you want your web service method to work with the provided JSON object, you will have to change your method signature to:

[WebMethod]
LoginSpecial(string Username, string Password)
{
}

JSON, data AJAX, , -.

, - .NET -, :

jQuery POST - ASP.NET ASMX AJAX

, .

+4

:

Newtonsoft.Json.Linq = > JSON.net http://james.networking.com/projects/json-net.aspx

:

        System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream);

        string line = "";

        line = sr.ReadToEnd();

        JObject jo = JObject.Parse(line);

        string something = (string)jo["something"]
+2

Add a parameter to your method,

[WebMethod]
LoginSpecial(string something)
{
}
0
source

Source: https://habr.com/ru/post/1785453/


All Articles