Ajax Post returning "Object XMLHttpReqest" error
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.
精彩评论