<small id='xyHXj'></small><noframes id='xyHXj'>

      • <bdo id='xyHXj'></bdo><ul id='xyHXj'></ul>
      <tfoot id='xyHXj'></tfoot>
      <i id='xyHXj'><tr id='xyHXj'><dt id='xyHXj'><q id='xyHXj'><span id='xyHXj'><b id='xyHXj'><form id='xyHXj'><ins id='xyHXj'></ins><ul id='xyHXj'></ul><sub id='xyHXj'></sub></form><legend id='xyHXj'></legend><bdo id='xyHXj'><pre id='xyHXj'><center id='xyHXj'></center></pre></bdo></b><th id='xyHXj'></th></span></q></dt></tr></i><div id='xyHXj'><tfoot id='xyHXj'></tfoot><dl id='xyHXj'><fieldset id='xyHXj'></fieldset></dl></div>
      <legend id='xyHXj'><style id='xyHXj'><dir id='xyHXj'><q id='xyHXj'></q></dir></style></legend>
    1. 启用 CORS AngularJS 以发送 HTTP POST 请求

      Enable CORS AngularJS to send HTTP POST request(启用 CORS AngularJS 以发送 HTTP POST 请求)
        • <i id='ddrKb'><tr id='ddrKb'><dt id='ddrKb'><q id='ddrKb'><span id='ddrKb'><b id='ddrKb'><form id='ddrKb'><ins id='ddrKb'></ins><ul id='ddrKb'></ul><sub id='ddrKb'></sub></form><legend id='ddrKb'></legend><bdo id='ddrKb'><pre id='ddrKb'><center id='ddrKb'></center></pre></bdo></b><th id='ddrKb'></th></span></q></dt></tr></i><div id='ddrKb'><tfoot id='ddrKb'></tfoot><dl id='ddrKb'><fieldset id='ddrKb'></fieldset></dl></div>
              <tbody id='ddrKb'></tbody>
          • <small id='ddrKb'></small><noframes id='ddrKb'>

            <tfoot id='ddrKb'></tfoot><legend id='ddrKb'><style id='ddrKb'><dir id='ddrKb'><q id='ddrKb'></q></dir></style></legend>

                <bdo id='ddrKb'></bdo><ul id='ddrKb'></ul>
                本文介绍了启用 CORS AngularJS 以发送 HTTP POST 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我想通过向位于不同域的服务器提交表单来发送 HTTP POST 请求(使用 node.js 在服务器脚本中启用 cors).

                I want to send an HTTP POST request by submitting a form to my server, which is located at a different domain (enabled cors in the server script using node.js).

                这是所有 Angular 配置所在的脚本:

                This is the script where all the Angular configurations are :

                var myApp = angular.module('myApp', ['ngRoute']);
                
                myApp.config(function($routeProvider, $locationProvider, $httpProvider) {
                
                  $httpProvider.defaults.useXDomain = true;
                  delete $httpProvider.defaults.headers.common['X-Requested-With'];
                
                  $routeProvider
                  .when('/', {
                    controller: 'RouteCtrl',
                    templateUrl: 'views/home_views.html'
                  })
                  .when('/login', {
                    controller: 'RouteCtrl',
                    templateUrl: 'views/login_views.html'
                  })
                  .when('/register', {
                    controller: 'RouteCtrl',
                    templateUrl: 'views/register_views.html'
                  })
                });
                
                myApp.controller("UserController", function($scope, $http) {
                  $scope.formData = {};
                  $scope.clickMe = function() {
                    console.log("Yay");
                      $http({
                        method: 'POST',
                        url: 'http://localhost:8183/user/register',
                        data: $.param($scope.formData),
                      })
                      .success(function(data) {
                        console.log(data);
                        if(!data.success) {
                          console.log("error here");
                        } else {
                          console.log("error there");
                        }
                      });
                  }
                }); ...
                

                我正在使用 AngularJS 1.2.22,正如本教程中所述(启用 CORS) 启用 CORS,需要在配置中手动启用 CORS.但它仍然无法正常工作.这是我从浏览器控制台得到的.

                I'm using AngularJS 1.2.22 and as it stated in this tutorial (Enable CORS) to enable CORS, it needs to enable CORS manually in the config. But it's still not working. Here is what I got from the browser console.

                跨源请求被阻止:同源策略不允许在 http://localhost:8183 读取远程资源/用户/注册.这可以通过将资源移动到同一域或启用 CORS 来解决.

                Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8183/user/register. This can be fixed by moving the resource to the same domain or enabling CORS.

                我对 AngularJS 还是很陌生,所以如果能指出我犯的任何错误,我将不胜感激.谢谢!

                I'm quite new to AngularJS so any help would really be appreciated to point out any mistakes I made.. Thank you!

                ---- 添加 server.js 脚本----

                var express = require('express'),
                    app = express(),
                    bodyParser = require('body-parser'),
                    expressValidator = require('express-validator'),
                    mysql = require('mysql'),
                    crypto = require('crypto'),
                    cors = require('cors'),
                    uuid = require('node-uuid');
                
                var connectionpool = mysql.createPool({
                    connectionLimit: 1000,
                    host: 'localhost',
                    user: 'root',
                    password: '',
                    database: 'cloudvm'
                });
                
                app.listen(8183);
                app.use(bodyParser.urlencoded({
                    extended: true
                }));
                
                app.use(bodyParser.json());
                app.use(expressValidator());
                app.use(cors());
                
                
                var user_router = express.Router();
                var user_list = user_router.route('/list');
                var user_register = user_router.route('/register');
                var user_login = user_router.route('/login');
                
                app.use('/user', user_router);
                
                user_register.post(function(req, res, next) {
                
                    var errors = req.validationErrors();
                    if (errors) {
                        res.status(200);
                        res.send(errors);
                        console.log(errors);
                        return;
                    }
                    var data = {
                        name_user: req.body.name,
                        email_user: req.body.email,
                        password_user: req.body.password,
                        no_telp_user: req.body.no_telp,
                        company_name_user: req.body.company_name,
                        address_user: req.body.address,
                        name_cc_user: req.body.name_cc,
                        address_cc_user: req.body.address_cc,
                        no_cc_user: req.body.no_cc,
                        no_vcv_user: req.body.no_vcv,
                        expire_month_cc_user: req.body.expire_month,
                        expire_year_cc_user: req.body.expire_year
                    };
                
                    connectionpool.getConnection(function(err, connection) {
                        if (err) {
                            console.error('CONNECTION ERROR:', err);
                            res.statusCode = 503;
                            res.send({
                                result: 'error',
                                err: err.code
                            });
                        } else {
                            var sql = 'INSERT INTO user SET ?';
                            console.log(sql)
                            connection.query(sql, data, function(err, rows, fields) {
                                if (err) {
                                    console.error(err);
                                    res.statuscode = 500;
                                    res.send({
                                        result: 'error',
                                        err: err.code
                                    });
                                }
                                res.send([{
                                    msg: "registration succeed"
                                }]);
                                connection.release();
                            });
                
                        }
                
                    });
                });
                

                解决方案

                感谢您的友好回答,但我已经设法在我的服务器脚本(在 Node 上运行)上启用了 CORS,然后我尝试使用它

                Thank you for the kind answers, but I've managed to enable CORS on my server script (running on Node) then I tried to use this

                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                

                在调用 http 请求时在我的客户端脚本上,然后它终于让我从服务器获得响应而不会出现 CORS 问题!所以,我认为这可能是标题问题.. 所以,感谢您的友好回复!希望这对以后遇到此问题的人有所帮助!

                on my client-side script when the http request is called, then it finally let me to get response from the server without having the CORS problem! So, I thought it might be the header problem .. So, thank you for kind responses! Hope this would help anyone having this problem in the future!

                推荐答案

                这就是我在 express 应用程序中执行 CORS 的方式,您必须记住 OPTIONS,因为对于某些框架,有 2 个 CORS 调用,第一个是 OPTIONS,它检查什么方法可用,然后有实际调用,OPTIONS 只需要空答案 200 OK

                That's how I do CORS in express applications, you have to remember about OPTIONS because for some frameworks there are 2 calls for CORS, first one is OPTIONS which checks what methods are available and then there is actual call, OPTIONS require just empty answer 200 OK

                allowCrossDomain = function(req, res, next) {
                  res.header('Access-Control-Allow-Origin', '*');
                  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
                  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
                  if ('OPTIONS' === req.method) {
                    res.send(200);
                  } else {
                    next();
                  }
                };
                
                app.use(allowCrossDomain);
                

                这篇关于启用 CORS AngularJS 以发送 HTTP POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
                XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
                Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
                How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
                quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)
                How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)

                <tfoot id='tHDJm'></tfoot>

              • <legend id='tHDJm'><style id='tHDJm'><dir id='tHDJm'><q id='tHDJm'></q></dir></style></legend>

                <small id='tHDJm'></small><noframes id='tHDJm'>

                  <tbody id='tHDJm'></tbody>

                    <bdo id='tHDJm'></bdo><ul id='tHDJm'></ul>
                      1. <i id='tHDJm'><tr id='tHDJm'><dt id='tHDJm'><q id='tHDJm'><span id='tHDJm'><b id='tHDJm'><form id='tHDJm'><ins id='tHDJm'></ins><ul id='tHDJm'></ul><sub id='tHDJm'></sub></form><legend id='tHDJm'></legend><bdo id='tHDJm'><pre id='tHDJm'><center id='tHDJm'></center></pre></bdo></b><th id='tHDJm'></th></span></q></dt></tr></i><div id='tHDJm'><tfoot id='tHDJm'></tfoot><dl id='tHDJm'><fieldset id='tHDJm'></fieldset></dl></div>