Reputation: 1491
I'm trying to call a .NET asmx web service using jQuery. I've been using guides here and here and as far as I can tell I've followed them to the letter.
Service Code:
[WebService(Namespace = "http://tempuri.org/", Description = "...")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class MyService : WebService
{
private static readonly IKernel NinjectKernel = new StandardKernel(new IocModule());
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public string HelloWorld(string name)
{
return string.Format("Hello {0}", name);
}
I can happily browse to the service in Firefox and invoke the HelloWorld method.
Client jQuery:
if (ajaxRunning) {
return;
}
ajaxRunning = true;
var webMethod = "http://localhost:51546/MyService.asmx/HelloWorld";
var inputname = "Jack";
$("[id$='spinner']").show();
$("[id$='spinnerText']").show();
$.ajax({
type: "POST",
url: webMethod,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {name: inputname},
success: function (msg) {
$("[id$='spinner']").hide();
$("[id$='spinnerText']").hide();
ajaxRunning = false;
alert(msg.d);
},
error: function() {
$("[id$='spinner']").hide();
$("[id$='spinnerText']").hide();
ajaxRunning = false;
alert("Fail");
}
});
When I run the javascript there are no errors in Firebug, just the Fail alert pop-up. Please tell me if I am doing something obviously wrong?
Thanks in advance
Upvotes: 0
Views: 2255
Reputation: 312
Need to stringify the parameters being sent to the WebService. The:
data: {name: inputname}
Need to be replaced with:
data: JSON.stringify({name: inputname})
Upvotes: 1