2013-08-20 32 views
1
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调用?

回答

4

您应该尝试使用$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') 
+0

非常感谢,有用! –

相关问题