开发者

Issue reading googleapi response using jquery

I want to use jQuery function to fetch web service response but getting error 405 saying method not allowed . When I copy paste the same string in the web browser I get xml result without error. below is the code I am using

[
 var arr= new Array();
        var loopCounter = 0;
        alert("Before Ajax")
        $.ajax({
            type: "GET",
            url开发者_如何学运维: "http://maps.googleapis.com/maps/api/directions/xml?origin=ajax&destination=toronto&region=ca&avoid=tolls&sensor=false",
            dataType: "xml",
            success: function(xml) {
                alert("I am working");
                $(xml).find('distance').each(function() {
                    arr[loopCounter] = $(this).find('text').text();
                    alert("Some Value Of Distance = "+arr[loopCounter]);
                    loopCounter += 1;
                });

            }
        });]


What you need to use is JSONP for cross domain requests:

If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.


this is caused by the same origin policy, which means that you can only use Ajax to fetch a url with the same origin(include domain, port, protocal)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜