【问题标题】:set crossDomain: true in angularJS http.get() method在 angularJS http.get() 方法中设置 crossDomain: true
【发布时间】:2016-04-12 09:34:30
【问题描述】:

我必须使用 Angular JS $http.get 方法调用一个 ajax 请求,但它显示错误

跨域请求被阻止:同源策略不允许读取位于http://example.com 的远程资源。 (原因:缺少 CORS 标头“Access-Control-Allow-Origin”)。

我的方法是

$http.get(url).success( function(response) {
   // some thing here
});

有什么方法可以在get方法中设置crossDomain: true

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    有以下选项:

    1. 使用JSONP
    2. 在服务器上添加'Access-Control-Allow-Origin' : '*'
    3. 使用带有 '--disable-web-security' 标志的 Chrome。

    【讨论】:

      【解决方案2】:

      希望您不要试图通过浏览本地 html 文件来调用 get 方法。尝试通过 http-server 启动它,然后你甚至可以使用 jsonp

      【讨论】:

        【解决方案3】:

        你必须允许cors:

        .config(function($httpProvider){ 
            $httpProvider.defaults.useXDomain = true;
            $httpProvider.defaults.headers.common["Accept"] = "application/json";
            $httpProvider.defaults.headers.common["Content-Type"] = "application/json";
            $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
        
        })
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-28
          • 1970-01-01
          • 2015-11-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多