AngularJS 提供了 $http.jsonp
方法用于发送 JSONP 请求。JSONP 成功函数不起作用可能是由于以下原因:
下面是一个解决方法的代码示例:
// 定义一个 AngularJS 控制器
angular.module('myApp', [])
.controller('myController', function($scope, $http) {
$scope.loadData = function() {
var url = 'http://example.com/api?callback=JSON_CALLBACK';
$http.jsonp(url)
.then(function(response) {
// 成功回调函数
$scope.data = response.data;
})
.catch(function(error) {
// 错误回调函数
console.log('Error:', error);
});
};
});
在上面的代码中,我们使用 $http.jsonp
方法发送一个 JSONP 请求,并通过 .then
方法处理成功回调函数和 .catch
方法处理错误回调函数。
确保替换 url
变量为正确的 JSONP 请求 URL,其中 callback=JSON_CALLBACK
是 AngularJS 内部生成的回调函数名称。
如果上述解决方法无效,你可能需要检查服务器是否正确处理 JSONP 请求,并确保返回的数据格式是正确的。