Reputation: 10493
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.
Upvotes: 4
Views: 4480
Reputation: 5606
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
Upvotes: 3
Reputation: 10493
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.
Upvotes: 0
Reputation: 108040
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>
Upvotes: 0
Reputation: 125538
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
This example demonstrates how to create a bridge to flickr through the flickr API.
Upvotes: 3
Reputation: 38156
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.
Upvotes: 0
Reputation: 74560
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).
Upvotes: 1