cagin cagin - 7 months ago 7
Javascript Question

jQuery ajax call doesn't work with parameters

I want to call a web service method. here is my jQuery code:

var name = $("#Name").val();
alert(name);
$.ajax({
type: "POST",
url: "CityList.aspx/Edit",
data: '{"name":' + name + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);

},
error: function (msg) {
alert(msg);
}
});


And here is my server side code:

[WebMethod]
public static void Edit(string name)
{
...
}


When I pass the parameter, it is falling to error but I call the method without parameter everything is ok. Do you have any suggestions?

Answer

I changed my data text with: data: '{name:"' + name + '",id:' + id + '}' and it resolved. Thanks for you helps.