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

前端 未结 6 1361
粉色の甜心
粉色の甜心 2020-12-17 04:19

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 awar

相关标签:
6条回答
  • 2020-12-17 05:08

    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.

    0 讨论(0)
  • 2020-12-17 05:11

    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.

    0 讨论(0)
  • 2020-12-17 05:13

    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>
    
    0 讨论(0)
  • 2020-12-17 05:14

    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.

    0 讨论(0)
  • 2020-12-17 05:17

    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

    0 讨论(0)
  • 2020-12-17 05:17

    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).

    0 讨论(0)
提交回复
热议问题