Ajax call pass parameters to function
What is wrong with this code? I am trying to pass parameters to a WCF function. I coul开发者_JAVA百科dn't get this to work. I am getting Ajax error.
$.ajax({
url: applicationPath + "/Test.svc/GetData",
type: "POST",
dataType: "json",
data: '{GId":' + sender.get_value() + '"GName":' + sender.get_text() + '"mId":' + testId + '}',
contentType: "application/json; charset=utf-8",
success: function(result)
{
//trying to fill combobox here
},
});
The data is not valid JSON. What follows is the code and an example of its result.
'{GId":' + sender.get_value() + '"GName":' + sender.get_text() + '"mId":' + testId + '}'
>> '{GId":'foo'"GName":'bar'"mId":5}'
Instead of constructing JSON manually, I would use JSON.stringify
and pass it an object.
JSON.stringify({GId: sender.get_value(), GName: sender.get_text(), mId: testid})
>> "{"GId":"df","GName":"sdf","mId":4}"
I think you can see there is a difference. Your code was missing commas and a quotation mark at the beginning of GId
.
精彩评论