jQuery AJAX loads JSONP from localhost but doesn't return data

半城伤御伤魂 提交于 2019-12-12 01:33:07

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/14478985/jquery-ajax-loads-jsonp-from-localhost-but-doesnt-return-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!