views:

374

answers:

3

I apologize if this has been asked before. I searched but did not find anything. It is a well-known limitation of AJAX requests (such as jQuery $.get) that they have to be within the same domain for security reasons. And it is a well-known workaround for this problem to use iframes to pull down some arbitrary HTML from another website and then you can inspect the contents of this HTML using javascript which communicates between the iframe and the parent page.

However, this doesn't work on the iPhone. In some tests I have found that iframes in the Safari iPhone browser only show content if it is content from the same site. Otherwise, they show a blank content area.

Is there any way around this? Are there other alternatives to using iframes that would allow me to pull the HTML from a different domain's page into javascript on my page?

Edit:
One answer mentioned JSONP. This doesn't help me because from what I understand JSONP requires support on the server I'm requesting data from, which isn't the case.

That same answer mentioned creating a proxy script on my server and loading data through there. Unfortunately this also doesn't work in my case. The site I'm trying to request data from requires user login. And I don't want my server to have to know the user's credentials. I was hoping to use something client-side so that my app wouldn't have to know the user's credentials at the other site.

I'm prepared to accept that there is no way to accomplish what I want to do on the iPhone. I just wanted to confirm it.

+2  A: 

You generally can NOT inspect the contents of an iframe from another domain via JavaScript. The most common answers are to use JSONP or have your original server host a proxy script to retrieve the inner contents for you.

Given your revisions, without modification or support from the secondary site, you are definitely not going to be able to do what you want via the iPhone's browser.

Tracker1
Thanks for the response. I have looked into both JSONP and hosting a proxy script. Neither work in my particular situation I will edit my question to explain more.
jkohlhepp
A: 

Make the device the proxy; pretend that the iPhone is your server. That way you can keep the username/ password on the device itself.

peterp
I'm sorry I am afraid I have no idea what you mean. Can you explain in more detail? How would I "pretend that the iPhone is my server"? I'm not writing an iPhone app it is only a web page hosted in mobile safari.
jkohlhepp
I assumed you're writing a native app with a UIWebView hosted inside the app.
peterp
A: 

"In some tests I have found that iframes in the Safari iPhone browser only show content if it is content from the same site"

I found the same thing. Is this documented somewhere? Is there a workaround? This sounds like broken web standards to me, and I am wondering if there is a solution.

David