ajax - 如何使用 AngularJS $http 配置元素进行跨站点域

标签 ajax angularjs angularjs-directive angularjs-scope angular-ui-router

我正在尝试在 angularjs 1.3.15 中对跨站点域进行 AJAX 调用。我现在有一个标准的 AJAX 调用正在工作,但想改用 Angular 的 $http 配置元素。

我现在的 AJAX 调用:

var responsePromise = $.ajax({url:"https://some-address.com",
                              type: "GET",
                              dataType: "json",
                              xhrFields: {withCredentials:true},
                              crossDomain: true});
                              
                              //my success and error functions

我用 $http(config) 尝试过的内容

var repsonsePromise = $http.get('/some-address.com',{reponseType:{json},
                                                     withCredentials:{'true'}});

//my .success and .error functions

我查看了 $http.get 服务 here

我应该为 crossDomain: true 参数添加什么,或者有人可以告诉我工作 AJAX 调用的等效语法才能使其正常工作吗?非常感谢您的帮助,先谢谢您。

最佳答案

试试这个:

app.config(function($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
});

取自:http://samurails.com/tutorial/cors-with-angular-js-and-sinatra/

关于ajax - 如何使用 AngularJS $http 配置元素进行跨站点域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29887192/

相关文章:

javascript - jQuery 对 ajax 查询字符串中的空格进行编码

javascript - JS/Ajax 警告框错误

javascript - 如何使用 jquery + ajax 制作多个依赖下拉列表

javascript - ajax请求的CORS错误

javascript - 是否有用于阅读更多/更少文本的基本 Angular Directive(指令)

javascript - 多个指令和 ng-transinclude

javascript - Angular Material 和 Safari 浏览器

angularjs - 如何将模板传递给属性中的指令?

javascript - 指令内容在包装 html 之前呈现

javascript - Angularjs指令如何检测属性的变化