开发者

Javascript using ajax jquery and json not working with IE or chrome help!

I have this code that is being ignored by i.e. ive set alerts throughout the code and i've pin point the error being the ajax call

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        <head>
        <title>Website</title>
<script type="text/javascript">

         $.ajax({
                url: "http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo",
                                      开发者_Python百科  success: function(data){
                                            ...code
        }
        });
</script>..body.. etc...

The code works perfectly on firefox.. however the $.ajax({}); call is completely ignored in IE and Chrome.. Anyone know why? Thanks in advance...


Are you missing <script> tags, and, you know, jQuery? Also, since this is a cross-site request, make sure you're actually handling this as jsonp correctly.

Without posting your actual code I think that's the best we can do.


I can only add to what Michael Haren has already said that what you get from this URL is:

{"status":{"message":"the daily limit of 30000 credits demo has been exceeded. Please throttle your requests or use the commercial service.","value":18}}


As pointed out, you're missing the script tags as well as the actual path to the jquery file, and you need to wrap your ajax call in with document ready so...

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> // Latest jquery from google
<script type="text/javascript">
     $(function() {
        // -- Your Code Here

     });
</script>


You have to tell add &callback=? at the end of the URL.

http://api.geonames.org/earthquakesJSON?...&username=demo&callback=?


One thing that will prevent the ajax from working in Chrome/Safari but still allow it to work in Firefox is leaving out the dataType field of the ajax command. For instance:

$.ajax({
    type: 'Get',
    url: "http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo",
    success: function(data){
        var jsonArray = jQuery.parseJSON(data);
        alert(jsonArray.status.message);
    }
});

will output the value element of the JSON array returned from that URL ONLY if ran in Firefox. To get this ajax to work in Chrome you must include the dataType field and remove the jQuery.parseJSON like so:

$.ajax({
    type: 'Get',
    url: "http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo",
    dataType: 'json',
    success: function(data){
        var jsonArray = data;
        alert(jsonArray.status.message);
    }
});

If this is left out or you try to do it the other way it will appear as it is skipping over your ajax all together. I hope this fixes your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜