...

View Full Version : Ajax in Safari



cameron213
08-23-2010, 02:16 AM
I am writing an application for the iphone that parses a weather feed based on gps location. I have the following which grabs coordinates based on a program called instamapper and posts them online. I am able to parse the necessary information and can print it to the screen in IE. However, as the iphone uses safari I want to see what my output is. How would I be able to do this? I've been running my code in an html file.




<html>
<script = "Javascript">



function fetchgps(callback)
{
var url = "http://www.instamapper.com/api?action=getPositions&key=584014439054448247";

var myRequest = new XMLHttpRequest();
myRequest.onreadystatechange = function(e) {gps_xml_loaded(event, myRequest, callback);}
myRequest.open("GET", url);
myRequest.setRequestHeader("Cache-Control", "no-cache");
myRequest.setRequestHeader("wx", "385");
myRequest.send(null);

return myRequest;
}

function gps_xml_loaded(event, this_request, callback)
{

if (this_request.readyState == 4){

if (this_request.status == 200) {

var obj = {error:false, errorString:null}

var data = this_request.responseText;
if (data == null) {callback(constructError("no <data>")); return;}

collected=data.split(","); //parses the data delimited by comma and put data into array
obj.latitude = collected[3];
obj.longitude = collected[4];
callback(obj);
}
else
{

callback ({error:true, errorString:"Not Ready"}); //Could be any number of things..
}
}
}

function dealwithgps(obj)
{
if (obj.error == false){
lat = obj.latitude;
lon = obj.longitude;
document.write("Latitude "+lat);
document.write("Longitude "+lon);
}
else{
document.write("error detected");
}
}

fetchgps(dealwithgps);





</script>
</html>

A1ien51
08-27-2010, 10:03 PM
I would not use document.write. Put an element on the page and set its innerHTML with the content.

Have you added debug statements to see where it is failing?

Eric

Howard Rose
12-16-2011, 10:02 PM
I am writing an application for the iphone that parses a weather feed based on gps location. I have the following which grabs coordinates based on a program called instamapper and posts them online. I am able to parse the necessary information and can print it to the screen in IE. However, as the iphone uses safari I want to see what my output is. How would I be able to do this? I've been running my code in an html file.




<html>
<script = "Javascript">



function fetchgps(callback)
{
var url = "http://www.instamapper.com/api?action=getPositions&key=584014439054448247";

var myRequest = new XMLHttpRequest();
myRequest.onreadystatechange = function(e) {gps_xml_loaded(event, myRequest, callback);}
myRequest.open("GET", url);
myRequest.setRequestHeader("Cache-Control", "no-cache");
myRequest.setRequestHeader("wx", "385");
myRequest.send(null);

return myRequest;
}

function gps_xml_loaded(event, this_request, callback)
{

if (this_request.readyState == 4){

if (this_request.status == 200) {

var obj = {error:false, errorString:null}

var data = this_request.responseText;
if (data == null) {callback(constructError("no <data>")); return;}

collected=data.split(","); //parses the data delimited by comma and put data into array
obj.latitude = collected[3];
obj.longitude = collected[4];
callback(obj);
}
else
{

callback ({error:true, errorString:"Not Ready"}); //Could be any number of things..
}
}
}

function dealwithgps(obj)
{
if (obj.error == false){
lat = obj.latitude;
lon = obj.longitude;
document.write("Latitude "+lat);
document.write("Longitude "+lon);
}
else{
document.write("error detected");
}
}

fetchgps(dealwithgps);





</script>
</html>





There is nothing (obvious) that is wrong with your code. The problem is is with Apple. There Safari browser does not support AJAX. I am current looking for a solution for myself. Hope you have better luck than I have had so far.

Hoppy

Kor
12-23-2011, 10:50 AM
There is nothing (obvious) that is wrong with your code. The problem is is with Apple. There Safari browser does not support AJAX.
Who said so? Safari supports AJAX as all the other browsers.

And there are some errors in the code
1. Doctype. All the HTML documents must bear a Doctype, other wise the interpreter will approximate (in quirks mode) the way it should run the codes.
2.


<script = "Javascript">

No. Use the type (optional in HTML5)


<script type="text/javascript">

3. It is better to write the complete HTML structure of a document, to avoid any troubles:

- doctype here -
<html>
<head>

</head>
<body>

</body>
</html>
4. document.write() is not a dynamic method. Use DOM methods or innerHTML.

There is also a problem regarding the query passed to the request. If the query has special characters, the whole query should be encoded. See encodeURIComponent():
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent

And, the last but not the least:


var url = "http://www.instamapper.com/

AJAX can not perform a request cross-domains (except for IE, and only if the HTML document runs locally, which means it is not cross-domain). You might need a server-side proxy application for that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum