Search code examples
jqueryasp.netajaxwebmethodpagemethods

JQqery ajax call with parameter not working


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..


Solution

  • 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.