I made a jquery ajax call for calling a static page method. It is working fine wihtout any parameter. But if I put parameter then it does not call to that page method. I have below code.

JAVASCRIPT

        $.ajax({
        type: 'POST',
        url: 'ItemMaster.aspx/UploadFile',
        contentType: 'application/json; charset=utf-8',
        data: {'path':'mydata'},
        dataType: 'json',
        success: function (msg) {
            alert(msg.d);
        }
    });

PAGE METHOD

    [WebMethod]
    public static string UploadFile(string path)
    {
        return "Success";
    }

Is there any datatype mismatching happened?. I warm up google for some time without any success. Please help..

有帮助吗?

解决方案

Your data object needs to be a JSON string. Try

var dataToSend = JSON.stringify({'path':'mydata'});

$.ajax({
    type: 'POST',
    url: 'ItemMaster.aspx/UploadFile',
    contentType: 'application/json; charset=utf-8',
    data: dataToSend,
    dataType: 'json',
    success: function (msg) {
        alert(msg.d);
    }
});

Be sure to include JSON.js if you are supporting older browsers.

其他提示

The data you are sending is not json. Either remove the content type, or convert the data to json.

I'd remove the content type.

$.ajax({
    type: 'POST',
    url: 'ItemMaster.aspx/UploadFile',
    data: {path:'mydata'}, // you may need to remove the quotes from path here
    success: function (msg) {
        alert(msg.d);
    }
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top