Bobbake4
Bobbake4

Reputation: 24857

jQuery getJSON is throwing error?

I am using the getJSON function in a file to get JSON from an api I am creating but the getJSON always enters my error handler for some reason. The code for the javascript is here. It always enters the .error function.

    window.onload = function() 
            {
                var data = "store_id=5";
                $.getJSON('http://localhost:81/javascript_plugin/get_store_items', data,
                function(json) {
                  alert(json);
                })
                .success(function() { alert("second success"); })
                .error(function() { alert("error"); })
                .complete(function() { alert("complete"); });
            }

I am using CakePHP for my api side stuff and the file that is returned is shown below. $content_for_layout contains a JSON object created by calling "echo $js->object($json);" using the jQuery as the javascript engine. $json is a result of a CakePHP find call.

    <?php
         header("Pragma: no-cache");
         header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate"); 
         header('Content-Type: text/x-json'); 
         header("X-JSON: ".$content_for_layout);
         echo $content_for_layout; 
    ?>

When in Firebug the response code is 200 and the header contains all of the stuff shown but the response tab is empty. Any help would be appreciated.

Thanks

Upvotes: 2

Views: 1045

Answers (1)

Bobbake4
Bobbake4

Reputation: 24857

My problem was solved by moving the client side html file to my web server and running it from there. It seems that running a 'file://' html file will not allow this type of call to a server. Now all versions of jQuery work as well as all browsers.

Upvotes: 1

Related Questions