在我的 php 中,我确实喜欢这样回显 jsonp 类型“json data”
echo $_GET['callback'] . '('.json_encode($arr).')';
在我的 js (angularjs) 中我这样做
$http.get('http://example.com/app/?callback').
success(function(data, status, headers, config) {
console.log(data);
});
但是我收到了这个错误
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.
最佳答案
如果我没记错的话,您需要指定 JSON_CALLBACK
作为回调参数并使用 $http.jsonp()
或 $http({method : 'jsonp'})
如果您想将 jsonp 与 $http
一起使用。
您没有为回调参数指定任何内容,并且正在尝试使用 $http.get()
。
尝试一下:
$http.jsonp('http://example.com/app/?callback=JSON_CALLBACK').
success(function(data, status, headers, config) {
console.log(data);
});
关于javascript - Jsonp 出现 No 'Access-Control-Allow-Origin' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28926243/