0

I'm trying to load some JSONP from my localhost. I have a PHP webapp that returns JSON with a callback if it is provided. The JSON that is returned is 100% valid (checked with JSON Validator). The url looks like this:

http://localhost/backstage/public/data/acties?callback=?

Now when I try to load this data with jQuery AJAX, it gives me the alert saying 'error', implying that the loading has failed.

var url = "http://localhost/backstage/public/data/acties";

$("#debug").click(function() {
    console.log("getting data from " + url);
    $.ajax({
       type:'GET',
       url: url,
       dataType:'jsonp',
       success: function(data){
           alert('loaded');
       },
       error: function(data){
           alert('error');        
       }
    });
});

However, when I go to the network tab I see a request has been made to:

http://localhost/backstage/public/data/acties?callback=jQuery19008035339566413313_1358941083680&_=1358941083681

And the content in this file is valid JSON (again, I validated it using JSON Validator). The data just doesn't seem to end up in my data variable in JavaScript.

For reference, this is my PHP (Zend 1.12) code:

public function actiesAction()
    {
        $data = new Application_Model_DbTable_Actie();
        $data = $data->fetchAll();
        $callback = htmlspecialchars($_GET["callback"]);
        $data = Zend_Json::encode($data);

        echo $callback.'('.$data.');';     
    }

and this is the JSONP that you get when you go to

http://localhost/backstage/public/data/acties?callback=?

--> http://pastebin.com/tazcUQAW

Does anyone know how to fix this please?

thomastuts
  • 3,459
  • 3
  • 20
  • 28

1 Answers1

0

I found the error: my base layout was still including HTML code in the response. I followed the steps in handle JSONP calls in ZEND (second answer) and now it works perfectly. Thanks for the help anyway!

Community
  • 1
  • 1
thomastuts
  • 3,459
  • 3
  • 20
  • 28