Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST JSON data to .asmx webservice

I'm trying to post some simple parameters to a .asmx webservice.
I get the following error: Request format is invalid: application/json; charset=utf-8.
What I really need to get to is to be able to pass a complex object, but I can't get past making a POST request with json content type.

Here is my WebService Definition

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public int JsonTest2(int myparm1, int myparm2)
{
    return 101;
}

And this is my javascript code

function JsonTest2() {
    $.ajax({
        type: 'POST',
        url: "http://localhost/WebServices/MyTest.asmx/JsonTest2",
        data: "{myparm1:105,myparm2:23}",
        contentType: 'application/json; charset=UTF-8',
        dataType: 'json',
        async: false,
        success: function (msg) {
            alert(msg);
        },
        error: function (msg) {
            alert('failure');
            alert(msg);
        }
    });
}
like image 894
swandog Avatar asked Mar 16 '11 03:03

swandog


1 Answers

Make sure your ASMX service class is decorated with the [ScriptService] attribute.

like image 184
Dave Ward Avatar answered Sep 23 '22 06:09

Dave Ward