Ajax - Accessing web Service from jQuery - cross domain

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I am trying to acess a wcf service from a jQuery client

Specifically this example http://www.codeproject.com/KB/aspnet/WCF_JQUERY_ASMX.aspx#4 http://www.codeproject.com/KB/aspnet/WCF_JQUERY_ASMX.aspx#4

All works well when the client webpage is on the same domain as the service

As soon as I move the client webpage to another domain it breaks. It cant reach the service and the request fails

This happens for all the examples, ASMX, REST and WCF

any ideas how to get this working cross daomain?

Answers

You are running up against the Same-Origin Policy. The web service that you are accessing must reside on the same domain as the jQuery script that is making the request. This policy is enforced by all browsers to prevent - for example - cross-site scripting and code injection attacks on web applications.

There are various ways around it, including JSONP, Proxies or Flash.

http://ajaxian.com/archives/jsonp-json-with-padding http://ajaxian.com/archives/jsonp-json-with-padding

http://taossa.com/index.php/2007/02/08/same-origin-policy/ http://taossa.com/index.php/2007/02/08/same-origin-policy/

https://developer.mozilla.org/En/Same_origin_policy_for_JavaScript https://developer.mozilla.org/En/Same_origin_policy_for_JavaScript

Here s an example use of JSONP:

url = "http://www.test.com/getData.php?callback=parseResults";

document.body.appendChild((function() {
    var newScript = document.createElement("script");
    newScript.type = "text/javascript";
    newScript.src = url;
    return newScript;
})());

function parseResults(data) {
    alert(data);
}

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/2697557/accessing-web-service-from-jquery-cross-domain

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils