0

I use the protocol jsonp to call web methods.

I use this code for the webservice:

public class Service1 : System.Web.Services.WebService
{
    [WebMethod]
    [ScriptMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
}

And this on Jquery with jason on client side:

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

            success: function(Msg) {
                alert('success:' + Msg.d.FirstName);

            },
            error: function(xhr, textStatus, errorThrown) {
                alert("error");
            }

        });

    }

This Jquery gives me always an error message, but I don't know the reason. Someone can help me?

5
  • 2
    Change alert("error") to alert(textStatus + " - " + errorThrown) for starters :) Commented Sep 9, 2010 at 19:31
  • Why do you use url like 'localhost:50837/Service1.asmx/HelloWorld' and not like '/Service1.asmx/HelloWorld'? If web service on the other web site and your JavaScript program? Where you use jsonp in your current code? Commented Sep 9, 2010 at 19:32
  • I have 2 projects one with WS and another with page asp. I need to call the WS like this 'localhost:50837/Service1.asmx/HelloWorld', because if i put url: '/Service1.asmx/HelloWorld' give me: error not found 404 Commented Sep 9, 2010 at 19:42
  • the errorThrown give-me undefined ... i dont know why Commented Sep 9, 2010 at 19:47
  • 1
    You should place Web Service as a part of the site. If you don't do this your code will not work because of Same Origin Policy problem (see en.wikipedia.org/wiki/Same_origin_policy). You can place on the same site many application ASP.NET MVC, ASPX web service and WCF service and all can work together very well. It's the only way to be able without more complex JSONP. In you situation JSONP is oversized. Commented Sep 9, 2010 at 19:56

1 Answer 1

2

You should place Web Service as a part on the site. If you don't do this your code will not work because of Same Origin Policy problem (see ).

You can place on the same site many application developed with different technique like ASP.NET MVC, ASMX Web Service and WCF service and all can work together very well. It's the only way to be able without more complex JSONP. In you situation JSONP is oversized.

You question is almost the same as Calling simple web service (.asmx file) from AJAX and JQuery using JSON - parse error. If you need a working Hello Wold example you will find an url to the full code example.

You can read more about different ways of solving Same Origin Policy under Question about making XHR requests. After reading of that you will understand that Same Origin Policy problem is really complex. You can solve it, but in your case you not really have it need just place all on the same web site and use relative paths.

Sign up to request clarification or add additional context in comments.

1 Comment

ok, you're right. To solve my problem i call a function on server side and call the WS. Thanks for help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.