访问控制允许原点未被识别

时间:2014-04-24 18:17:03

标签: javascript node.js angularjs

我正在编写这个node.js应用程序,我有一个奇怪的情况:

这段Angularjs ......

// delete hosts
$scope.delete = function(row) {
    var rowData = {
        hostname: row.hostname,
        ipaddr: row.ipAddress,
        location: row.location,
        vp: row.vp,
        virthost: row.virtHost,
        services: row.services
    };

    var delurl = "http://10.2.201.6:666/deleteserver";
    var jdata = "mydata="+JSON.stringify(rowData);

    $http({
        method: method,
        url: delurl,
        data: jdata,
        //headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        cache: $templateCache
    }).
    success(function(response) {
        console.log("success");
        $scope.codeStatus = response.data;
        console.log($scope.codeStatus);
    }).
    error(function(response){
        console.log("error");
        $scope.codeStatus = response || "Request failed";
        console.log($scope.codeStatus);
    });
    $scope.list();
    return false;

};

正在调用此node.js位...

//// DELETE
app.delete('/deleteserver', function (req, res) {
    console.log("DELETE: ");
    res.header("Access-Control-Allow-Origin", "http://10.2.201.6");
    res.header("Access-Control-Allow-Methods", "GET, POST");

    console.log(req.body);
    console.log(req.body.mydata);
    var row = JSON.parse(req.body.mydata);

    db.servers.remove(row, function(err, result) {
        if(err) { throw err; }
        res.end(console.log("Record deleted"));
    });


});

但由于某些原因,我在控制台中遇到以下错误:

XMLHttpRequest cannot load http://10.2.201.6:666/deleteserver. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://10.2.201.6' is therefore not allowed access.

我把头撞在一堵墙,一张桌子和一扇窗户上,试图找出为什么忽略“Access-Control-Allow-Origin”。是的,侦听端口666是正常的。事实上,我的GET和POST方法具有相同的 res.header(...)代码,并且它们的工作非常出色。 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您不需要将DELETE添加到res.header(" Access-Control-Allow-Methods"," GET,POST");

另外,你不想res.send没有res.end?