当前位置: 首页 > 面试题库 >

Nodejs和AngularJS的CORS问题

霍浩皛
2023-03-14
问题内容

由于某种原因,每当我尝试将一些数据发布到api服务器时,都会出现以下两个错误。

OPTIONS http://localhost:3000/test2 Request header field Content-Type is not allowed by Access-Control-Allow-Headers. angular.min.js:99
XMLHttpRequest cannot load http://localhost:3000/test2. Request header field Content-Type is not allowed by Access-Control-Allow-Headers.

这是我的客户端有角JS代码,试图发送一些简单数据。该代码当前正在位于以下位置的Nginx服务器上运行http://localhost:8080

function Controller($scope, $http) {
    //scope is all of the elements within the controller declared on the html
    var url = 'http://localhost:3000/test2';

    $scope.listVaules = function () {
        console.log("about to post user id");
        console.log($scope.user.userId);
        console.log($scope.user.name);
        console.log($scope.user.password);
        console.log(JSON.stringify($scope.user));
        $http({ method: 'Post', url: url, data: JSON.stringify($scope.user) }).
            success(function (data, status, headers, config) {
                console.log(data);
                console.log('success');
            }).
            error(function (data, status, headers, config) {
                console.log('error');
            });
    };
    }

这是我的节点JS代码,用于处理“处理”请求 localhost:3000/test2

// CORS header securiy
app.all('/*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
   res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  next();
});

//Should post client side json info to the server
app.post(url + '/test2', function(req, res) {
    var name = req.body.name;
    var userId = req.body.userId;
    var password = req.body.password;

    console.log(name + ' ' + userId + ' ' + password);
    res.send(200);
});

问题答案:

如错误消息所述,您必须在对预检的响应中确认Content-Type标头。这可能是由于您的请求的非“简单” Content-
Type(表面上是application / json)引起的。

因此,代替此:

res.header("Access-Control-Allow-Headers", "X-Requested-With");

…你需要这个:

res.header("Access-Control-Allow-Headers", "X-Requested-With, Content- Type");



 类似资料:
  • 我将vuejs与nodejs一起使用,vue客户端地址,nodejs服务器地址。 响应中的错误是

  • 我已经搜索了200多个站点(可能有些夸张,但没有太多),了解如何使用angularjs处理cors。我们有一台运行web API服务器的本地计算机。我们正在开发一个客户端,该客户端调用API获取数据。从服务器运行客户机时,我们接收数据没有问题。当我们从另一个域运行它时,当我们试图获取数据时,会得到一个红色200响应和一个空白响应。下面是一些代码: 我也尝试过使用$http,但得到了相同的响应(或没

  • 问题内容: 我已经搜索了200多个站点(可能有点夸张,但幅度不大),以了解如何使用angularjs处理cors。我们有一台运行Web API服务器的本地计算机。我们正在开发一个调用API数据的客户端。从服务器运行客户端时,我们不会收到任何问题。当我们从其他域运行它时,尝试获取数据时会收到红色的200响应,而响应为空。这是一些代码: 我也使用$ http尝试过此操作,但得到的响应相同(或缺少响应)

  • 我正在设置Angular Spring Security模块来登录和注册用户。当我注册一个用户时,一切都正常。注册后的最后一步是自动登录,但我遇到了以下错误: XMLHttpRequest无法加载超文本传输协议//localhost:8080/com-tesis/login.请求的资源上不存在“访问控制允许起源”标头。因此不允许访问起源“超文本传输协议//localhost:9000”。响应的HT

  • 问题内容: 我目前正在尝试在NodeJS + React App中实现Auth0。尽管存在一个大问题,但给出的本教程确实非常有用且很有帮助。每次我尝试通过Auth0登录/注册时,都会收到 XMLHttpRequest无法加载 https://XYZ.eu.auth0.com/usernamepassword/login。对预检请求的响应未通过访问控制检查:在所请求的资源上不存在“ Access-C

  • 问题内容: 我有一个基于@RestController和AngularJS的简单项目。我可以将Angular的GET请求发送到@RestController,但无法发送POST请求。我在浏览器控制台中出错: XMLHttpRequest无法加载http:// localhost:8080 / add 。Access-Control-Allow- Headers不允许请求标头字段Content-Ty