使用AJAX将数组发送到NodeJS的另一种方法

时间:2017-03-26 15:19:38

标签: javascript ajax node.js

所以我在使用AJAX向NodeJS发送数组时遇到了很多问题,当使用JSON发送数组时,错误函数总是被调用(问为什么这给了我没有答案我可以使用)。

所以我想知道是否有人知道不同的方法,我现在拥有的是:

$.ajax({
    type: 'POST',
    url: 'http://localhost:1337/deposit?steamid=' + steamid,
    contentType: "application/json; charset=UTF-8",
    dataType: 'json',
    data: JSON.stringify({arr:items}),
    success: function(data) {
        console.log("Tradeoffer has been sent");
    },
    error: function(data) {
        alert("Failed to call bot, please inform us about this issue by DM'ing us at https://twitter.com/csgobeararms");
        console.log("Failed to call bot, please inform us about this issue by DM'ing us at https://twitter.com/csgobeararms");
    }
});

在服务器端:

app.post('/deposit', function(req, res) {
   console.log('Deposit request recieved, info:');
   console.log('STEAM ID: ' + req.query.steamid);
   console.log('ITEMS: ' + req.body.arr);
});

所以我想知道是否有人能告诉我另一种发送阵列的方式。

如果你能告诉我这段代码有什么问题,那当然会很棒。

2 个答案:

答案 0 :(得分:0)

app.js

// nodejs v4.2.6

var express = require('express');
var app = express();
var fs = require('fs');

// Add headers
app.use(function (req, res, next) {

    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', '*');

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);

    // Pass to next layer of middleware
    next();
});
app.get('/test', function(req, res) {
fs.readFile('test.html',function (err, data){
        res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
        res.write(data);
        res.end();
    });
});
app.post('/deposit', function(req, res) {
   console.log('Deposit request recieved, info:');
   console.log('STEAM ID: ' + req.query.steamid);
   //console.log('ITEMS: ' + req.body.arr);
   res.send({'finish':'finish'});
});

app.listen(3000, function(){    
      console.log('http Express server(worker) listening on port 3000');
});

的test.html

<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.0.min.js"></script>
</head>
<body>
<script>
var steamid = 1;
$.ajax({
    type: 'POST',
    url: 'http://localhost:3000/deposit?steamid=' + steamid,
    contentType: "application/json; charset=UTF-8",
    dataType: 'json',
    data: JSON.stringify({arr:[1,2,3,4]}),
    success: function(data) {
        console.log("Tradeoffer has been sent");
    },
    error: function(data) {
        alert("Failed to call bot, please inform us about this issue by DM'ing us at https://twitter.com/csgobeararms");
        console.log("Failed to call bot, please inform us about this issue by DM'ing us at https://twitter.com/csgobeararms");
    }
});
</script>
</body>
</html>

答案 1 :(得分:-2)

我认为您在Node中收听错误的网址。在AJAX中,您可以调用url:http://localhost:1337/deposit?steamid=' + steamid,,但是您可以在Node中收听'/deposit'。 您可以尝试使用RegExp:

app.post(new RegExp('/deposit?steamid=[a-zA-Z0-9]')

(假设`steamid只包含字母数字字符)。

由于您未发送GET请求,因此您可以删除网址中的steamid参数并致电http://localhost:1337/deposit/' + steamid,;然后,您应该在节点中收听app.post(new RegExp('/deposit?steamid=[a-zA-Z0-9]')

希望这有帮助!