0

I have this in my web service;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld()
    {
        return "Hello Worlds";
    }
}

And this as my jQuery;

    $(document).ready(function () {
        $.support.cors = true;

        $.ajax({
            type: "POST",
            url: "http://localhost:61614/Service1.asmx/HelloWorld",
            data: "{}",
            dataType: "json",
            success: function (msg) {
                alert(0);
                alert(msg);
            }, error: function (a,b,c) { alert(c); }
        });
    });

When I run, my breakpoint in the web service fires and I return "Hello Worlds".

However, on return back to the jQuery I drop into the error function. Safari simply alerts an empty string and IE alerts "No Transport".

Can anyone see what I'm doing wrong?

1 Answer 1

0

You also need to add ScriptMethod attribute:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public string HelloWorld()
{
    return "Hello Worlds";
}

Also, you need to specify contentType in your ajax call:

$.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "http://localhost:61614/Service1.asmx/HelloWorld",
            data: "{}",
            dataType: "json",
            ...

Also, a good article on the topic: Here.

4
  • i added that descriptor but the problem still persists
    – griegs
    May 4, 2012 at 5:04
  • If you go to http://localhost:61614/Service1.asmx in the browser and run your method from there, does it work? There might be some mis-configuration on the server side. I am using a bunch of web service methods like yours from client side (js) code without any problems with the exact code I showed you.
    – Andrey
    May 4, 2012 at 6:05
  • Also, go to the article I linked - in the bottom they have a complete downloadable solution, with server side and client side, see if it works in your environment; if it doesn't then it's a sign that something's wrong with your environment. Please update me on your progress.
    – Andrey
    May 4, 2012 at 6:08
  • thanks for taking the time. Still not working. I'm going to close this question I think and Open a new more relevant one as it's an issue across two applications. Seems this might be a x-domain issue.
    – griegs
    May 6, 2012 at 23:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.