AngularJS:$ http不适用于外部API

瓦西里·马热金(Vasiliy Mazhekin)
myApp.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
 }]);

$http.defaults.useXDomain = true;

$http.get('http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=asp+site:codehint.ru').
    success(
        function (data, status, headers, config) {
            alert('answer');
        }
   );

不管用!因为:Access-Control-Allow-Origin不允许使用Origin \ site name \。

我刚刚开始使用AngularJS并被迫使用$ .getJSON,因为在上述情况下它可以正常工作。有人可以向我展示上述代码的正确解决方案,以对Google之类的外部服务进行AJAX调用吗?

洛朗·佩林(Laurent Perrin)

您应该改用$ http.jsonp

您只需要添加callback=JSON_CALLBACK到您的请求中:

$http.jsonp('http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=asp+site:codehint.ru&callback=JSON_CALLBACK')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章