Using JQuery To make cross domain Ajax Calls
Below code does not seem to work when my asmx webservice is in a different domain. IS there anything I am missing?
function CallGreet()开发者_开发问答 {
$.ajax({
type: "POST",
url: "http://test.me/TestWebService.asmx/HelloWorld",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
error: OnError
});
}
Thanks...
You need to know about JSONP
Here you will find some more details: How to call external webservice using jquery "jsonp"?
Also note that what jsonp jquery result will be is the same as this answer by @Craig White : Using JQuery To make cross domain Ajax Calls
I prefer to include a javsacript file generated from the other domain that can give me the information.
<script type="text/javascript" src="http://otherdomain.com/data.php"></script>
you can dynamically add a script like this:
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'http://otherdomain.com/data.php?×tamp=' + new Date().getTime(); //Added to deter caching
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(s);
Thr browser prevent JavaScript from make ajax calls to another domain.
What you can do, is that you create a local asp file on your domain, which reads the data on the other domain and call this.
Your problem is all browser block cross site requests. You need to have the page on the same one as the server, or set your server to allow cross site requests. Take a look at http://www.easywms.com/easywms/?q=en/how-send-cross-site-request-ajax
精彩评论