views:

38

answers:

3

I'm trying to create an ajax connection to a weather xml feed and then parse returned data. I don't have any issues making a connection via IE, but for some reason I don't have any luck in FF or Safari. Basically what I'm doing is running an html file that contains the following code.

<html> 
<script type="text/javascript" language="javascript">

    function makeRequest(zip) {  
    var url = 'http://rdona.accu-weather.com/widget/rdona/weather-data.asp?location=' + zip;
         //var httpRequest; 
    var httpRequest = false; 

         if (window.XMLHttpRequest) { 
    document.write("xmlhttprequest");
        httpRequest = new XMLHttpRequest();  
             if (httpRequest.overrideMimeType) {  
                 httpRequest.overrideMimeType('text/xml');  
                 // See note below about this line  
             }  
         }   
         else if (window.ActiveXObject) { // IE  
             try {  
                 httpRequest = new ActiveXObject("Msxml2.XMLHTTP");  
             }   
             catch (e) {  
                 try {  
                     httpRequest = new ActiveXObject("Microsoft.XMLHTTP");  
                 }   
                 catch (e) {}  
             }  
         }  

         if (!httpRequest) {  
             alert('Giving up :( Cannot create an XMLHTTP instance');  
             return false;  
         }  
         httpRequest.onreadystatechange = function() { alertContents(httpRequest); };  
         httpRequest.open('GET', url, true);  
         httpRequest.send('');  

     }  

     function alertContents(httpRequest) {  

         if (httpRequest.readyState == 4) {  
             if (httpRequest.status == 200) {  
                alert(httpRequest.responseText);  
             } else {  
                 alert('There was a problem with the request.');  
             }  
         }  

     } 

makeRequest(84405); 
</script>  
</html>

Any help and or suggestions would be greatly appreciated.

A: 

For cross browser stuff, I recommend you use a library like JQuery because it will quietly smooth over IE vs Firefox vs Safari etc issues. Also to make the code format properly, use the 101010 button in the toolbar.

MattSmith
+1  A: 

I strongly suggest you use a framework to do this sort of thing. Frameworks will do all of the browser compatibility stuff for you.

On the other hand, if you are interested in how to do this as an academic exercise... still get a framework! See how the framework does it and you will immediately learn all of the pitfalls.

Mootools is my framework of choice.

In order to perform a basic AJAX request in Mootools you would do the following:

window.addEvent('domReady', function() {
    new Request({
        'url': "The url where you want to send the request
        'data': "Some data to send. It can be an object."
    }).send();
});

Full documentation for the Request class can be found here.

If you want to see how Mootools implements cross-browser AJAX, you can find the source of the Request class here.

You'll find the source for Browser.Request particularly useful.

Rupert
This is for an iphone project not sure if this will work for my purposes.
cameron213
I'm not familiar with iphone development. However, if iphone apps understand JavaScript then Mootools has a server version which should run inside an iphone app just fine. Just put it at the top of your code.
Rupert
@cameron - It should work. Mobile Safari's standard support is excellent, and I don't expect it to differ from the desktop version in terms of ajax support too much.
Yi Jiang
+1  A: 

I'm afraid you will run into some issues because of same origin policy, meaning that you cant do XMLHTTPRequests to another domain.

Not even jQuery (which you really should check out anyways) can help you with that.

Kristoffer S Hansen