سؤال

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