开发者

Grabbing external URL's HTML via jQuery/AJAX

开发者 https://www.devze.com 2023-02-13 12:09 出处:网络
I need to grab a specific DOM element (by ID) from an external URL via jQuery/AJAX. I know that this violates the same origin policy, but I can\'t find an acceptable workaround for when I\'m specifica

I need to grab a specific DOM element (by ID) from an external URL via jQuery/AJAX. I know that this violates the same origin policy, but I can't find an acceptable workaround for when I'm specifically grabbing HTML (instead of JSON).

So say for example, I'm on website1.com, and I ne开发者_如何学JAVAed to grab and output the contents of the #something element of website2.com. How can I do this using jQuery?

EDIT: Seems to be a duplicate of Why cant I load an external resource from jQuery load method? which has a good answer.


(Answering my own question):

Note that this is impossible in the browser because of the same origin policy. To get around it (like Pekka said in the comments above), you'd normally create a local proxy of the desired content. So like if you are working on website.com, and you need to grab a part of google.com, you could create website.com/google which would just be a clone of google.com via some server side code. Then you'd be free to do whatever you wanted to it, since it's on your domain.

The catch I was dealing with is that I was working on a BlackBerry WebWorks project, which means that I'm limited to HTML/CSS/JS, and I'm not actually on a website, so a proxy is impossible. Luckily, WebWorks lets you add trusted domains to each app you create, which means that the same origin policy doesn't apply to those domains.


False. The policy is not absolute as long as the server supports JSONP. Google JSONP Jquery and you'll see tons of working examples doing what you want: returning webservice data from a domain other than your own via ajax.

0

精彩评论

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