开发者

Ajax Post returning "Object XMLHttpReqest" error

开发者 https://www.devze.com 2023-01-27 19:12 出处:网络
Can someone offer some help with my problem please? I have the following code: function InsertNewMeeting(tripID, contact1ID, contact2ID) {

Can someone offer some help with my problem please?

I have the following code:

        function InsertNewMeeting(tripID, contact1ID, contact2ID) {
        $.ajax({
            type: "POST",
            url: "RetrieveSites.aspx/InsertNewMeeting",
            data: "{'tripID': '" + tripID + "','contact1ID: '" + contact1ID + "','contact2ID', '" + contact2ID + "'}",
            contentType: "application/json; charset=utf-8", 
            datatype: "json",
            success: function (msg) { alert('ok'); },
            error: function (msg) {alert(msg);}
        });
    }

This is meant to call a WebService to post 3 integer values. The WebService works 100%.

However, when I'm calling the function from the web page, all I get is a "Object XMLHttpRequest" error.

I've examined all parameters that are passed, they are 100% ok.

Is there anythin开发者_如何学编程g obvious that I'm missing or should be doing?


Your single quotes in your string are all over the place.

The data you send will be this:

{'tripID': '[tripID]','contact1ID: '[contact1ID]','contact2ID', '[contact2ID]'}

Whereas it appears it should be this:

{'tripID': '[tripID]','contact1ID': '[contact1ID]','contact2ID': '[contact2ID]'}


You could do this to send a proper json object to the server:


function InsertNewMeeting(tripID, contact1ID, contact2ID) {
    var data = {'tripID': tripID, 'contact1ID': contact1ID, 'contact2ID': contact2ID};
    $.ajax({
        type: "POST",
        url: "RetrieveSites.aspx/InsertNewMeeting",
        data: JSON.stringify(data),
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        success: function (msg) {
            alert('ok');
        },
        error: function (msg) {
            alert(msg);
        }
    });
}

Mind you that you would need JSON2 to get JSON.stringify() in browsers that doesn't have support for it.

Another thing is that you'd need to enclose properties in double quotes, as it's not JSON if it's just single quotes. Try running your JSON-string through JSONLint to see if it validates.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号