jQuery AJAX从本地主机加载JSONP,但不返回数据 [英] jQuery AJAX loads JSONP from localhost but doesn't return data
问题描述
我正在尝试从本地主机加载一些JSONP.我有一个PHP webapp,如果提供了它,它会返回带有回调的JSON.返回的JSON是100%有效的(已通过JSON Validator检查).网址看起来像这样:
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=?
现在,当我尝试使用jQuery AJAX加载此数据时,它会提示我错误",表示加载失败.
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
此文件中的内容是有效的JSON(同样,我使用JSON Validator对其进行了验证).数据似乎并没有最终出现在我的JavaScript数据变量中.
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.
作为参考,这是我的PHP(Zend 1.12)代码:
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.');';
}
这是您转到
http://localhost/backstage/public/data/acties?callback=?
-> http://pastebin.com/tazcUQAW
有人知道如何解决此问题吗?
Does anyone know how to fix this please?
推荐答案
我发现了错误:我的基本布局在响应中仍然包含HTML代码.我按照在ZEND中处理JSONP调用(第二个答案)中的步骤进行操作,现在可以正常工作了完美.无论如何,谢谢您的帮助!
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!
这篇关于jQuery AJAX从本地主机加载JSONP,但不返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!