Reputation: 5917
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 need 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.
Upvotes: 0
Views: 1614
Reputation: 38721
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.
Upvotes: 0
Reputation: 5917
(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.
Upvotes: 1