views:

93

answers:

1

Hi,

I use the protocol jsonp to call web methods, but i have this problem:

Have this code on 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 give me always the error message i dont know the reason. Someone can help me?

+1  A: 

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 http://stackoverflow.com/questions/3650561/calling-simple-web-service-asmx-file-from-ajax-and-jquery-using-json-parse-e/3651231#3651231. 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 http://stackoverflow.com/questions/3654197/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.

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