문제

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