Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Ajax in Safari

  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

  • #2
    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
    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]

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    Last edited by Kor; 12-23-2011 at 09:59 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •