views:

1087

answers:

6

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  A: 

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

casperOne
A: 

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.

Vasil
+2  A: 

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

Russ Cam
A: 

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>
Andreas Grech
Is that the client's web.config or the webservices?
Bravax
A: 

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.

Bravax
Use the [ScriptService] attribute on the webservice to allow it to be called from client side script. The default will return JSON - http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptserviceattribute.aspx
Russ Cam
Used in conjunction with JQuery's $.ajax({}); command in a similar fashion to call a webservice such as here - http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/ should do what you require
Russ Cam
+3  A: 

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

tanathos
+1, may need changes that the asker can't make, but will work cross-domain
orip