开发者

problem with jquery load

开发者 https://www.devze.com 2023-03-20 08:37 出处:网络
I set up a js fiddle - just a simple jquery lo开发者_开发百科ad function that loads the content of a url into a div, but it doesn\'t work. If I access the URL directly, it works fine. Any ideas what m

I set up a js fiddle - just a simple jquery lo开发者_开发百科ad function that loads the content of a url into a div, but it doesn't work. If I access the URL directly, it works fine. Any ideas what might be going wrong?

http://jsfiddle.net/heaversm/jLaPr/


The problem is that the url is on another server and you can't access it. If you want to call it, you should use $.ajax() and set the crossDomain option to true

Taken from the documentation of load():

Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.


Due to browser restrictions, most Ajax requests are subject to the "same origin policy". That means that in most cases, you can’t use jQuerys ajax methods to fetch data from external domains without using a Proxy, YQL, JSONP or equivalent technique to get around this.

You should consider using this: https://github.com/jamespadolsey/jQuery-Plugins/blob/master/cross-domain-ajax/jquery.xdomainajax.js

Using this plugin should allow the ajax example in your question.

Another option is to use a server-side proxy and then request that page using ajax. If your server can run PHP, try googling for something like "php ajax proxy" and you’ll get plenty results.


It's definitely a browser restriction (http://api.jquery.com/load#notes-0):

Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.

0

精彩评论

暂无评论...
验证码 换一张
取 消