pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    <legend id='HBcSt'><style id='HBcSt'><dir id='HBcSt'><q id='HBcSt'></q></dir></style></legend>

    1. <small id='HBcSt'></small><noframes id='HBcSt'>

      <i id='HBcSt'><tr id='HBcSt'><dt id='HBcSt'><q id='HBcSt'><span id='HBcSt'><b id='HBcSt'><form id='HBcSt'><ins id='HBcSt'></ins><ul id='HBcSt'></ul><sub id='HBcSt'></sub></form><legend id='HBcSt'></legend><bdo id='HBcSt'><pre id='HBcSt'><center id='HBcSt'></center></pre></bdo></b><th id='HBcSt'></th></span></q></dt></tr></i><div class="tscar2c" id='HBcSt'><tfoot id='HBcSt'></tfoot><dl id='HBcSt'><fieldset id='HBcSt'></fieldset></dl></div>
    2. <tfoot id='HBcSt'></tfoot>

      • <bdo id='HBcSt'></bdo><ul id='HBcSt'></ul>

      啟用 CORS AngularJS 以發(fā)送 HTTP POST 請求

      Enable CORS AngularJS to send HTTP POST request(啟用 CORS AngularJS 以發(fā)送 HTTP POST 請求)
      <tfoot id='xLUQd'></tfoot>
          <bdo id='xLUQd'></bdo><ul id='xLUQd'></ul>

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

            <legend id='xLUQd'><style id='xLUQd'><dir id='xLUQd'><q id='xLUQd'></q></dir></style></legend>
            • <i id='xLUQd'><tr id='xLUQd'><dt id='xLUQd'><q id='xLUQd'><span id='xLUQd'><b id='xLUQd'><form id='xLUQd'><ins id='xLUQd'></ins><ul id='xLUQd'></ul><sub id='xLUQd'></sub></form><legend id='xLUQd'></legend><bdo id='xLUQd'><pre id='xLUQd'><center id='xLUQd'></center></pre></bdo></b><th id='xLUQd'></th></span></q></dt></tr></i><div class="sn3ofl3" id='xLUQd'><tfoot id='xLUQd'></tfoot><dl id='xLUQd'><fieldset id='xLUQd'></fieldset></dl></div>

                  <tbody id='xLUQd'></tbody>

                本文介紹了啟用 CORS AngularJS 以發(fā)送 HTTP POST 請求的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我想通過向位于不同域的服務器提交表單來發(fā)送 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();
                            });
                
                        }
                
                    });
                });
                

                解決方案

                感謝您的友好回答,但我已經(jīng)設法在我的服務器腳本(在 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' }
                

                在調(diào)用 http 請求時在我的客戶端腳本上,然后它終于讓我從服務器獲得響應而不會出現(xiàn) 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 應用程序中執(zhí)行 CORS 的方式,您必須記住 OPTIONS,因為對于某些框架,有 2 個 CORS 調(diào)用,第一個是 OPTIONS,它檢查什么方法可用,然后有實際調(diào)用,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 以發(fā)送 HTTP POST 請求的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調(diào)用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調(diào)用完成)
                JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標頭) - IT屋-程序員軟件開發(fā)技術分
                Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內(nèi)容)
                Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)
                  <tfoot id='TwYXY'></tfoot>

                • <i id='TwYXY'><tr id='TwYXY'><dt id='TwYXY'><q id='TwYXY'><span id='TwYXY'><b id='TwYXY'><form id='TwYXY'><ins id='TwYXY'></ins><ul id='TwYXY'></ul><sub id='TwYXY'></sub></form><legend id='TwYXY'></legend><bdo id='TwYXY'><pre id='TwYXY'><center id='TwYXY'></center></pre></bdo></b><th id='TwYXY'></th></span></q></dt></tr></i><div class="9e3yw3o" id='TwYXY'><tfoot id='TwYXY'></tfoot><dl id='TwYXY'><fieldset id='TwYXY'></fieldset></dl></div>
                    <tbody id='TwYXY'></tbody>

                  1. <legend id='TwYXY'><style id='TwYXY'><dir id='TwYXY'><q id='TwYXY'></q></dir></style></legend>

                        <bdo id='TwYXY'></bdo><ul id='TwYXY'></ul>
                      • <small id='TwYXY'></small><noframes id='TwYXY'>

                          主站蜘蛛池模板: 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 鄂泉泵业官网|(杭州、上海、全国畅销)大流量防汛排涝泵-LW立式排污泵 | 郑州大巴车出租|中巴车租赁|旅游大巴租车|包车|郑州旅游大巴车租赁有限公司 | 垃圾处理设备_餐厨垃圾处理设备_厨余垃圾处理设备_果蔬垃圾处理设备-深圳市三盛环保科技有限公司 | RS系列电阻器,RK_RJ启动调整电阻器,RQ_RZ电阻器-上海永上电器有限公司 | 证券新闻,热播美式保罗1984第二部_腾讯1080p-仁爱影院 | 洛阳防爆合格证办理-洛阳防爆认证机构-洛阳申请国家防爆合格证-洛阳本安防爆认证代办-洛阳沪南抚防爆电气技术服务有限公司 | 环境模拟实验室_液体-气体控温机_气体控温箱_无锡双润冷却科技有限公司 | 懂研帝_专业SCI论文润色机构_SCI投稿发表服务公司 | 深圳南财多媒体有限公司介绍 | 量子管通环-自清洗过滤器-全自动反冲洗过滤器-北京罗伦过滤技术集团有限公司 | 清水-铝合金-建筑模板厂家-木模板价格-铝模板生产「五棵松」品牌 | 动库网动库商城-体育用品专卖店:羽毛球,乒乓球拍,网球,户外装备,运动鞋,运动包,运动服饰专卖店-正品运动品网上商城动库商城网 - 动库商城 | 冷热冲击试验箱_温度冲击试验箱价格_冷热冲击箱排名_林频厂家 | 混合生育酚_醋酸生育酚粉_琥珀酸生育酚-山东新元素生物科技 | 东莞市天进机械有限公司-钉箱机-粘箱机-糊箱机-打钉机认准东莞天进机械-厂家直供更放心! | 合景一建-无尘车间设计施工_食品医药洁净车间工程装修总承包公司 | 船用泵,船用离心泵,船用喷射泵,泰州隆华船舶设备有限公司 | 通风天窗,通风气楼,屋顶通风天窗,屋顶通风天窗公司 | 爱科技iMobile-专业的科技资讯信息分享网站| 隆众资讯-首页_大宗商品资讯_价格走势_市场行情 | 聚合氯化铝-碱式氯化铝-聚合硫酸铁-聚氯化铝铁生产厂家多少钱一吨-聚丙烯酰胺价格_河南浩博净水材料有限公司 | 物流之家新闻网-最新物流新闻|物流资讯|物流政策|物流网-匡匡奈斯物流科技 | 运动木地板_体育木地板_篮球馆木地板_舞台木地板-实木运动地板厂家 | 环压强度试验机-拉链拉力试验机-上海倾技仪器仪表科技有限公司 | 钢衬四氟管道_钢衬四氟直管_聚四氟乙烯衬里管件_聚四氟乙烯衬里管道-沧州汇霖管道科技有限公司 | 起好名字_取个好名字_好名网免费取好名在线打分 | 东莞动力锂电池保护板_BMS智能软件保护板_锂电池主动均衡保护板-东莞市倡芯电子科技有限公司 | 热回收盐水机组-反应釜冷水机组-高低温冷水机组-北京蓝海神骏科技有限公司 | 对辊式破碎机-对辊制砂机-双辊-双齿辊破碎机-巩义市裕顺机械制造有限公司 | 深圳宣传片制作_产品视频制作_深圳3D动画制作公司_深圳短视频拍摄-深圳市西典映画传媒有限公司 | 重庆波纹管|重庆钢带管|重庆塑钢管|重庆联进管道有限公司 | 精准猎取科技资讯,高效阅读科技新闻_科技猎 | 铝合金风口-玻璃钢轴流风机-玻璃钢屋顶风机-德州东润空调设备有限公司 | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | 广州物流公司_广州货运公司_广州回程车运输 - 万信物流 | 十二星座查询(性格特点分析、星座运势解读) - 玄米星座网 | 科研ELISA试剂盒,酶联免疫检测试剂盒,昆虫_植物ELISA酶免试剂盒-上海仁捷生物科技有限公司 | 河北凯普威医疗器材有限公司,高档轮椅系列,推车系列,座厕椅系列,协步椅系列,拐扙系列,卫浴系列 | PCB厂|线路板厂|深圳线路板厂|软硬结合板厂|电路板生产厂家|线路板|深圳电路板厂家|铝基板厂家|深联电路-专业生产PCB研发制造 | 汽液过滤网厂家_安平县银锐丝网有限公司|