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?

Christian Strempfer
  • 7,291
  • 6
  • 50
  • 75
Roger G
  • 1,575
  • 2
  • 10
  • 9
  • 2
    Change `alert("error")` to `alert(textStatus + " - " + errorThrown)` for starters :) – Nick Craver Sep 09 '10 at 19:31
  • Why do you use url like 'http://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? – Oleg Sep 09 '10 at 19:32
  • I have 2 projects one with WS and another with page asp. I need to call the WS like this 'http://localhost:50837/Service1.asmx/HelloWorld', because if i put url: '/Service1.asmx/HelloWorld' give me: error not found 404 – Roger G Sep 09 '10 at 19:42
  • the errorThrown give-me undefined ... i dont know why – Roger G Sep 09 '10 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 http://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. – Oleg Sep 09 '10 at 19:56

1 Answers1

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.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • ok, you're right. To solve my problem i call a function on server side and call the WS. Thanks for help. – Roger G Sep 10 '10 at 14:09