Adam
Adam

Reputation: 12660

Cross domain XMLHTTPRequest error

I'm trying to load XML data via the jQuery get method. The XML feed is located at the following URL: http://webservices.nextbus.com/service/publicXMLFeed?command=routeList&a=mbta

When I run the following code in the browser, I get an error:

XMLHttpRequest cannot load http://webservices.nextbus.com/service/publicXMLFeed?command=routeList&a=mbta. Origin http://173.203.89.156 is not allowed by Access-Control-Allow-Origin.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>Bus Map</title>
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0px; padding: 0px }
  #map_canvas { height: 100% }
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true">
</script>
<script type="text/javascript" src=" https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js">
</script>
<script type="text/javascript">

    $(document).ready(function() {
        var myLatlng = new google.maps.LatLng(42.3966499, -71.12188);
        var myOptions = {
            zoom: 14,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        $.get('http://webservices.nextbus.com/service/publicXMLFeed?command=routeList&a=mbta', function(stops) {
            alert(stops[0]);                                                                                                    
        })
    });

</script>
</head>

<body>
<div id="map_canvas" style="width: 100%; height: 100%"></div>
</body>
</html>

Is this domain categorically blocking all cross-domain XMLHTTPRequests, or is there away to bypass this error?

Upvotes: 0

Views: 1683

Answers (2)

Anthony -GISCOE-
Anthony -GISCOE-

Reputation: 608

If you need to get around the same-domain issue (like many APIs do), look into JSONP. I'm not that familiar with jQuery, but it appears to make JSONP calls with getJSON if the URL has a callback parameter.

Upvotes: 1

hvgotcodes
hvgotcodes

Reputation: 120198

the browser blocks cross domain xhrs. There is something called the Same-Origin-Policy you need to follow for xhrs, or you need to use an alternative, like server side proxying or jsonp if it is provided by the application.

Upvotes: 2

Related Questions