ndisdabest
ndisdabest

Reputation: 329

JSON not loading

Having some trouble getting a JSON feed to load from my site. I'm starting to think something is wrong with the feed itself, since plugging the address into a variety of different blocks of example code doesn't seem to work.

I've tried the examples below, and continually meet with "Origin null is not allowed by Access-Control-Allow-Origin" when trying to deep drill the error through Chrome's Javascript Console. Any ideas?

Try #1:

    <script type="text/javascript">
        $().ready(function(){ 
            var url = 'http://www.solidverbal.com/category/clicks?feed=json';
            $.get(url, function(data) {
                // can use 'data' in here...
            });
        });

    </script>

Try #2:

<script type="text/javascript">
        $.ajax({
            type: "POST",
            url: "http://www.solidverbal.com/category/clicks?feed=json",
            data: '{}', // your parameter goes here
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            processdata: true,
            success: function (msg) {
                loadDetails(msg.d);  // msg.d contains the JSON data being returned
            },
            error: function (msg, error, obj) {
                alert(msg.responseText);
            }
        });


        function loadDetails(results) {
            // depending on the data in the JSON object, you can access them using
            // the syntax results.<propertyname>  etc…
        }

    </script>

Upvotes: 0

Views: 878

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

Due to same origin policy restrictions you are not allowed to perform cross domain AJAX calls. So unless the page you are running this script from is hosted on http://www.solidverbal.com this won't work. As a possible workaround you could use JSONP if the remote domain supports it or supply a server side script on your domain that will serve as bridge between your domain and the remote domain and then perform the AJAX call to this script.

Upvotes: 1

Related Questions