0

我尝试了一个 Angular 演示并想玩 jsonp,但我无法让它工作,所以我将相同的代码粘贴到 jsbin 中,所以我可以问你们有什么问题。并注意它有效。

在我的开发站点上再次尝试了完全相同的代码。它不起作用。我可以在 webkit 中看到网络日志,我通过 http 代码 200 得到了一个有效的 json。但是没有执行回调。

我最终在同一个请求上尝试了 jQuery-Ajax,效果很好。为了我

$(document).ready(function() {
    $.getJSON('http://angularjs.org/greet.php?callback=?&name=Super%20Hero', function(data) {
        console.log(data); // Works
    });
});

这是我的代码的演示,它在本地 http://jsbin.com/ojibel/edit#html,live不起作用。

编辑:

我从 .html 切换到 .php 扩展名,它开始工作了。所以它必须与apache做些什么?

4

2 回答 2

1

in JQUERY api doc its mentioned that

"However, since JSONP and cross-domain GET requests do not use XHR, in those cases the jqXHR and textStatus parameters passed to the success callback are undefined."

I modified your code at http://jsbin.com/ojibel/3/edit

if you see i have created a function called getData and same function i am passing as call back URL and so i am able to display it in console.

The way JSONP works is it tries to add the URL as script tags and the out put of the URL is considered as script and that code executes as it is. so if you have call back xyx in output you will have something like

xyz(DATA_FROM_SERVER)

which will be same as

<script>xyz(DATA_FROM_SERVER)</script>
于 2012-07-04T09:58:09.837 回答
1

当您说它“不起作用”时,您能否描述您正在采取的步骤以及预期与实际结果是什么?

请注意,在您的示例中,您将 $http 与 $templateCache 一起使用,并且在 html 文件的底部有<script type="text/ng-template" id="http-hello.html">Hello, $http!</script>,它将使用http-hello.html请求响应填充缓存,因此如果您获取 /http-hello.html,将会有没有向服务器发出请求,并且将使用缓存的内容调用回调。

于 2012-07-04T14:19:13.673 回答