Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-23-2010, 01:16 AM   PM User | #1
cameron213
New to the CF scene

 
Join Date: Aug 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
cameron213 is an unknown quantity at this point
Ajax in Safari

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.

Code:
<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>
cameron213 is offline   Reply With Quote
Old 08-27-2010, 09:03 PM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
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
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 12-16-2011, 09:02 PM   PM User | #3
Howard Rose
New to the CF scene

 
Join Date: Dec 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Howard Rose is an unknown quantity at this point
Unhappy The problem is with Safari.

Quote:
Originally Posted by cameron213 View Post
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.

Code:
<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
Howard Rose is offline   Reply With Quote
Old 12-23-2011, 09:50 AM   PM User | #4
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Quote:
Originally Posted by Howard Rose View Post
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.
Code:
<script = "Javascript">
No. Use the type (optional in HTML5)
Code:
<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/Jav...deURIComponent

And, the last but not the least:
Code:
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.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 12-23-2011 at 09:59 AM..
Kor is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, iphone, javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:48 AM.


Advertisement
Log in to turn off these ads.