I’ve called methods like this using MVC with no problems for years. Whenever I try to call them via asmx web services I get all sorts of different errors. None of which provide any real clue as to what my problem is. Any help would be appreciated. I’ve tried adding ScriptMethod, changed attributes, etc. Nothing works. WTF am I missing here?
This is my jquery code
function savePerson() {
$.ajax({
type: "POST",
url: "Service.asmx/SavePerson",
data: "{'Name': 'Jim','Age': '32'}",
contentType: "application/json; charset=utf-8",
dataType:"json",
success: function (response) {
var data = response.d;
$('#output').html(data)
},
failure: function (msg) {
$('#output').text(msg);
}
});
This is my web service code
[WebMethod]
public string SavePerson(Person p)
{
return p.Name + " was saved successfully.";
}
It does need to be a
ScriptMethodin order to call it from JavaScript; I suspect that the other issue is your parameters – an ASMX method won’t do the automatic mapping that an MVC method will do, so you need to explicitly send an object with the appropriate name and fields, or implement a method that takes the properties individually and constructs an appropriate object server-side.