3

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.

This is the code I've 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've got 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'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.

0

3 Answers 3

4

If you want your web service method to work with the submitted JSON object, you'll have to change your method signature to:

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

The JSON object you supply to the data property of the AJAX call has to have property names matching the parameter names of your web service method signature.

if you want to submit a list of parameters, you have to submit an array of objects and create a wrapper .NET object in your web service as explained here:

Using jQuery to POST Form Data to an ASP.NET ASMX AJAX Web Service

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

2

On server side use the library :

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

And the code :

        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"]

Comments

0

Add the parameter to your method,

[WebMethod]
LoginSpecial(string something)
{
}

3 Comments

I tried that: public string LoginSpecial(string parameter) now when I call the webservice I get the followin error: Invalid web service call, missing value for parameter: \u0027parameter\u0027.", "StackTrace":"....
declare params like this in jquery data: { info = value, something = something},
ì have to look the exact syntacs up on monday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.