【发布时间】:2026-01-28 08:45:02
【问题描述】:
由于某种原因,我的 jsonp 代码无法使用 angular!我已经阅读了数千个示例,但它根本不起作用。请参阅下面的代码。
我已经阅读了这个问题,但它并没有解决我的问题Angularjs JSONP not working
我的示例代码显示了定义了成功和错误方法的 http.jsonp 调用。我目前正在使用 Chrome 进行开发和测试,第二张和第三张图片显示了 ajax 服务调用和来自服务的 jsonp 响应。
最新更新
在收到@TheSharpieOne 的帮助后,服务器响应现在返回一个实际函数(而不是之前出现问题的字符串),但由于某种原因没有执行成功方法?
$http.jsonp(url).then(function(response) {
alert('success'); // not hit
}, function onError(response) {
alert('error'); // this is displayed
});
非常感谢,
Angular JS
var url = serviceRoot + "api/customer/get?callback=JSON_CALLBACK";
$http.jsonp(url).success(function (data) {
alert('success'); // never called
})
.error(function () {
alert('error');
});
Chrome 网络面板
Shows chrome network panel, highlighting the ajax call
Ajax 响应
【问题讨论】:
-
控制台显示什么?
-
我们现在有了 CORS,为什么还要使用 JSONP 编写新的 API?
-
控制台窗口是空的,没有错误什么的。顺便问一下,CORS 是什么?谢谢,
-
在 devtools 的网络选项卡中的 URL 是什么样的(查询字符串和所有)?
标签: javascript angularjs