I have a basic HTML page that is nothing more than a starting point for an external script (It only contains a single html
, head
, script
tag). In this external script, I make an AJAX request to a Google API and then do some parsing with the returned data. When I'm done parsing, I put the modified JSON back out onto the DOM and the script terminates.
Everything works out fine on this end. The AJAX request to the Google API goes smoothly and in less than 3 seconds I have an HTML page with some JSON put into a pre
tag.
The problems start coming up when I attempt to make a request to this page from an iOS app. The problem is that no matter what I do, the data from the request always comes back with just the basic HTML page. The request is finished before the JSON can be put into the DOM because the AJAX request takes time.
I have tried all of the following methods from within the app to no avail.
- Straight up
+[NSData dataWithContentsOfURL:options:error:]
NSHTTPRequest
[NSURLConnection sendSynchronousRequest:returningResponse:error:]
NSURLRequest
I have used every different way to make a request from an iOS app but they all have the same problem. Every time the request is made to my page, only the basic HTML page is returned. Here is a sample output from all 4 of these methods.
<html>
<head>
<!-- Link to our custom script. Makes an AJAX request then puts JSON into the pre -->
<script src="mpservice.js"></script>
</head>
<pre id="output"></pre>
</html>
Notice how the pre
tag is empty because at the time of the request's termination, the AJAX call and parsing has not yet finished and I have not yet put the JSON into the DOM.
My question is as follows, How can I make a request from within an iOS app that will wait for any AJAX calls to finish before returning?
Thank you for your time and if I have not been specific enough, feel free to leave a comment. If you wish to view the page that I am talking about (for proof that JSON is eventually getting put into the pre tag, go to silicode.us/freeform
.