What is the best way to call a .net webservice using jquery?

笑着哭i 提交于 2020-01-10 02:12:06

问题


I'd like to call a .net webservice from another domain using only jquery.

What is the best way to do this? and are there any configuration changes I need to be aware of on the web site hosting the web page?

The reason I ask this, is that I am only marginally in control of that area. So I can only make limited changes.


回答1:


I think your problem is to make the crossdomain call. You have to change the data type of your jQuery request to jsonp.

Take a look at this link




回答2:


The browser does not allow XMLHTTPRequest calls across domains in its default configuration. You can change browser settings to make certain calls succeed, but this is considered bad practice.

In order to perform cross-domain requests, you can

  • Use the local server as a proxy to a remote server

    This example uses a local ASP.NET Web Service to make a call to the Yahoo! Geocode service

  • Use a bridge

    This example demonstrates how to create a bridge to flickr through the flickr API.




回答3:


Generally, the answer is no, assuming you are talking about ASPX Web Services (basically a WebService hosted in an ASP.NET site).

This is the first hit on Google when searching for "webservice call jquery" which should give you more info:

http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/ you are using to host the web service).




回答4:


First, I'm not really sure if cross-site ajax as implemented in jquery will work in all browsers (firefox 3) just like that. Second, I assume you are talking about a SOAP web service. I'd rather not do that. It'll be very complicated to implement.




回答5:


Here is an example:

$.post("CodersWS.asmx/DeleteBook", { id_book: parseInt(currBookID, 10) }, function(res) {
///do something with returned data: res
});

In the above example, I am calling a web service named CodersWS.asmx, and the WebMethod inside it called DeleteBook...I am also passing a parameter called id_book.

Also don't forget to add this snippet to your web.config, or else you wouldn't be able to access the web-service this way:

<system.web>
    <webServices>
        <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
        </protocols>
    </webServices>
</system.web>



回答6:


Would a better approach be to use: Jquery.getJSON?

See: JQuery.getJSON

This raises the question of how to output JSON compliant data using a webservice or similiar mechanism.



来源:https://stackoverflow.com/questions/426420/what-is-the-best-way-to-call-a-net-webservice-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!