簡體   English   中英

從本地主機向遠程服務器發出請求以獲取預檢請求'Access-Control-Allow-Origin'

[英]Making request to remote server from localhost getting preflight Request 'Access-Control-Allow-Origin'

完全錯誤是這樣的:

XMLHttpRequest無法加載http:// ip:port / path 對預檢請求的響應未通過訪問控制檢查:請求的資源上不存在“ Access-Control-Allow-Origin”標頭。 因此,不允許訪問源' http:// localhost:3000 '。

環境詳細信息:

精疲力盡的人幾乎嘗試了所有事情,但無法完成任務。

實際上,我是從angular js客戶端請求node.js服務器。 我嘗試了所有可能的選擇。

流程是這樣的:

  1. 從用angular js設計的Web門戶發起的請求。
  2. 點擊遠程計算機上的node.js服務器。

我嘗試過的

expressjs / cors解決localhost NodeJS + Express中的“ Access-Control-Allow-Origin”

客戶端代碼


$scope.testFunc = function () {
    $("div#preloader").show();
    $http.post(rtcControlUrl, data).success(function () {
        $("div#preloader").hide();
        if(response.success)
            $.notify(response.msg, "success");
        else $.notify(response.msg);
        console.log(response.data);
    }).error(function () {
        $.notify("Request timeout!");
        $("div#preloader").hide();
    });
};

app.post("/path", cors(), function(req, res) {
           shelljs.exec("bash test.sh",{silent:true,async:false}).output;
                    console.log(output);
                    res.json({"success": true, msg: 'some text', 'data' : output});
            }
    });

錯誤消息顯示對預檢請求的響應

如果您查看飛行前請求,您將看到它正在使用OPTIONS方法。

“預檢”請求首先通過OPTIONS方法向另一個域上的資源發送HTTP請求

您的代碼:

app.post(“ / path”,cors()

…僅在POST請求上使用cors中間件。

您還需要處理OPTIONS請求。

我通過將NPM請求模塊與請求標頭一起使用來解決此問題

{
   name : 'content-type',
   value: 'application/x-www-form-urlencoded'
}

現在,我無需在服務器端代碼上設置任何標題即可解決此問題。 我分三步實現了這一目標。

客戶

$scope.testFunction= function () {
        $("div#preloader").show();
        $http.get(url).success(function (response) {
            $("div#preloader").hide();
            if(response.success)
                $.notify(response.msg, "success");
            else $.notify(response.msg);
            console.log(response.data);
        }).error(function () {
            $.notify("Request timeout!");
            $("div#preloader").hide();
        });
    };

本地節點服務器

request({
        url    : url,
        method : "POST",
        json   : true,
        headers: [
            {
                name : 'content-type',
                value: 'application/x-www-form-urlencoded'
            }
        ],
        body   : data
    }, function optionalCallback(err, response) {
        if (err) {
            console.error('**************[page][functionName][Request][Fail]*****************', err);
            res.json({success: false, msg: 'msg', data: err});
        } else {
            var dataObj = (response && response.body) ? response.body.data : undefined;
            console.info('*************************[pageName][functionName][Request][Success]**********************');
            res.json({success: true, msg: 'msg', data: ''});
        }
    });

記住在本地節點服務器標頭上的方式與 本視頻中 所述的相同

我要在其上執行最終業務邏輯的終端服務器。

var cors = require('cors');
app.options('*', cors());
app.post("/path", cors(), function(req, res) {
           shelljs.exec("bash test.sh",{silent:true,async:false}).output;
                    console.log(output);
                    res.json({"success": true, msg: 'some text', 'data' : output});
            }
    }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM