AngularJS中不存在"Access-Control-Allow-Origin"标头 [英] No 'Access-Control-Allow-Origin' header present AngularJS

查看:69
本文介绍了AngularJS中不存在"Access-Control-Allow-Origin"标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个快速演示站点,该站点无法控制我要连接的服务器.这是我使用AngularJS构建它的代码.我正在通过一个简单的Python HTTP Server运行该文件,并在localhost:8000上查看它.var RetrieveAppliances = function(){console.log('正在尝试检索设备列表.');

I am trying to build a quick demo site that I do not have control over the server I am trying to connect to. Here is the code that I am using to build it with AngularJS. I am running the file through a simple Python HTTP Server and viewing it at localhost:8000. var retrieveAppliances = function () { console.log('Attempting to retrieve appliance list.');

var requestUrl = '****';
    $http({
        method: 'GET',
        url: requestUrl,
    })
    .then(function (response) {
        console.log(response);
    });
};
retrieveAppliances(); 

我已经阅读了多个地方,尝试将方法切换为 JSONP ,但是这样做会导致解析错误.

I have read multiple places to try switching the method to JSONP but doing so resulted in a parsing error.

虽然我曾考虑尝试构建 server.js 文件并使用它运行NodeJS,但我在学习提出AJAX请求并将其代理到我的app.js的基础知识方面并未成功.

While I have considered trying to build a server.js file and running NodeJS with it, I am unsuccessful in learning the basics of making an AJAX request and proxying that to my app.js.

我将不胜感激,因为有人可以通过明确且易于执行的步骤为我提供任何帮助.

I will greatly appreciate any help that someone may be able to give me, with clear and easy to follow steps.

推荐答案

如果您正在对另一个来源(例如,不同的主机,端口或协议)运行Ajax调用,而该来源的服务器不支持交叉原始请求,那么您将无法从客户端修复该请求.客户无能为力.

If you're running an Ajax call to a different origin (e.g. different host, port or protocol) and the server at that origin does not have support for cross origin requests, then you cannot fix that from your client. There is nothing you can do from the client.

如果服务器支持JSONP,则可以使用它,但这还需要特定的服务器支持.

If the server supported JSONP, you could use that, but that also requires specific server support.

浏览器网页上唯一的解决方案是:

The only solutions from a browser web page are:

  1. 目标服务器上的CORS支持.
  2. JSONP(还需要目标服务器上的支持).
  3. 设置您自己有权访问的服务器(在现有页面域上或使用CORS),然后让该服务器为您获取文件/数据并将其代理回给您.您可以编写自己的代理,也可以部署预构建的代理.
  4. 找到一些可以使用的现有第三方代理服务.

如果您对制作自己的node.js代理感兴趣,可以在此处看到一个简单的示例:如何创建简单的示例是node.js中的http代理?.

If you're interested in making your own node.js proxy, you can see a simple example here: How to create a simple http proxy in node.js?.

这篇关于AngularJS中不存在"Access-Control-Allow-Origin"标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆