0

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

asked Jun 5, 2013 at 10:28

1 Answer 1

1

Need to stringify the parameters being sent to the WebService. The:

data: {name: inputname}

Need to be replaced with:

data: JSON.stringify({name: inputname})
answered May 19, 2014 at 12:55
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.