处理节点js中的跨域ajax帖子 [英] handle cross domain ajax post in node js

查看:316
本文介绍了处理节点js中的跨域ajax帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的app.js有以下处理程序

My app.js has the below handler for post

app.all('/', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    next();
});

app.options('*', cors());

var 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');
    next();
};

app.use(allowCrossDomain);

var whitelist = ['http://myurl.net'];

var corsOptions = {
    origin: function (origin, callback) {
        var originIsWhitelisted = whitelist.indexOf(origin) !== -1; callback(null, originIsWhitelisted);
    }
};
app.post('/ValidateUser/users',cors(corsOptions) , function (req, res) {
    var db = req.db;
    var collection = db.get('userlist');

    collection.find({}, {}, function (e, docs) {
        res.json(docs);
    });

    res.send("Hey");
});

调用此HTML的html页面具有以下代码

The html page which calls this has the below code

var user = { "bla":"bla"  };

function checkUser() {
    $.ajax({
        type:"POST",
        url: 'http://localhost:1337/validateuser/users',
        data: user,
        crossDomain: true,
        dataType:"jsonp"
        //jsonp: false,
        //jsonpCallback: 'jsonCallback'
    }).success(function (data) { console.log(data) });
};

当我保持断点并检查 req.body ,它没有req.body中的参数未定义

When i keep breakpoint and check the req.body , it is not having the parameters in the req.body which is undefined

我无法获取我从节点的html中发布的数据js

I am not able to fetch the data that i am posting from the html in the node js

推荐答案

使用body-parser模块获取数据,例如:

Use body-parser module to fetch the data, for example:

server.js:

server.js:

var express = require('express');
var bodyParser = require('body-parser');
var db = require('./db'); //module that contains db connection config

var app = express();

app.use(bodyParser());

app.get('/', function(req, res) {
    res.sendFile('/index.html', {root: __dirname });
});

app.post('/users', function(req, res) {
    console.log(req.body); //req.body contains the user = {bla: 'bla'} object
    var user = req.body;
    var collection = db.get('userlist');
    collection.find(user, function (e, docs) {
        res.json(docs);
    });
    res.send("Hey");
});

var server = app.listen(3000, function() {
    console.log('listening');
});

index.html

index.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

    <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script>
    var user = { "bla":"bla"  };
    function checkUser(data) {
        $.ajax({
            type:"POST",
            url: 'http://localhost:3000/users',
            data: data,
            dataType: "json"
        }).success(function (data) { console.log(data) });
    };
    checkUser(user);
    </script>

</body>
</html>

这篇关于处理节点js中的跨域ajax帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆