如何通过进行ajax调用将html表单数据发送到node.js服务器?

时间:2022-03-13 06:40:11

I want to send form data to node.js server using Ajax and I am fallowing the below approach to send data.

我想使用Ajax将表单数据发送到node.js服务器,我正在采用以下方法来发送数据。

I am not getting how receive it in node.js server program I am not using express framework for node.js

我没有得到如何在node.js服务器程序中接收它我没有使用node.js的快速框架

client.HTML

<script>
 function myFunction() {
   var region = document.getElementById("region").value;
   var os = document.getElementById("os").value;
   var data = {};
   data.region = region;
   data.os = os;
   $.ajax({
     type: 'post',
     datatype: 'jsonp',
     data: JSON.stringify(data),
     contentType: 'application/json',
     url: 'http://127.0.0.1:8083/', //node.js server is running
     success: function(data) {
       alert("success");

     }
   });

</script>
<form>
<select id="region" name="region" class="region"></select>
<select id="os" name="os" class="os"></select>  
<input type="button" value="search" class="fil_search" onclick="myFunction()"/>
</form>

server.js

var http = require('http');
var fs = require('fs');
var url = require('url');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert')
var ObjectId = require('mongodb').ObjectID;
var express = require('express');
var bodyParser = require('body-parser');
var app     = express();
var result1=[];
var result2=[];
var result3=[];
var result4=[];
var result5=[];
var result6=[];
var result7=[];
var i=0;
var region;
var os;
app.use(bodyParser.urlencoded({ extended: true })); 

MongoClient.connect("mongodb://192.168.1.22:27017/test", function(err, db) {

if(err) { return console.dir(err); }
else {
app.get('/',function(req, res) {

var url_parts = url.parse(req.url, true);
var url_parsed = url.parse(req.url, true);

"i want data here from ajax to perform"

console.log("connected");


var instance = db.collection('instance');
var region = db.collection('region');

region.findOne(({$and: [{"region_name": region_name},{"os_type": os}]}), function(err, result){
if(err){    
throw(err);
}
else{
console.log(region);
var newr = result.inst_name;

instance.find({ "inst_id": { "$in": newr } }).toArray(function(err, resultn){
if(err){
throw(err);
}
else{       
    var len=resultn.length;

    console.log(resultn);
    console.log(len);

    for(var i=0;i<len;i++)
    {
        result1[i]=resultn[i].inst_type;
        result2[i]=resultn[i].vcpu;
        result3[i]=resultn[i].memory_gib;
        result4[i]=resultn[i].storage;
        result5[i]=resultn[i].phy_processor;
        result6[i]=resultn[i].clock_spd;
        result7[i]=resultn[i].netwk_pef;            
    }
    var wstream = fs.createWriteStream('myOutput.txt');
    wstream.write(result1.toString()+"~"+result2.toString());       
    //var str = "Hi man"
    res.writeHead(200, {'Content-Type':'text/html'});
    //res.end(url_parsed.query.callback+'("'+resultn.toString()+'")'); 
    res.end(url_parsed.query.callback+'("'+result1.toString()+"~"+result2.toString()+"~"
    +result3.toString()+"~"+result4.toString()+"~"+result5.toString()+"~"+result6.toString()
    +"~"+result7.toString()+'")');      
}
});
}
}); 
}).listen(process.env.PORT || 8083);

}
});

I am not getting how to receive data in node.js server program and after receiving data I want process on that data and send back processed data to same HTML page.

我没有得到如何在node.js服务器程序中接收数据,并且在接收数据后我想对该数据进行处理并将处理后的数据发送回相同的HTML页面。

1 个解决方案

#1


1  

If you have app.use(bodyParser.urlencoded({ extended: true })); in your server.js file then using bodyParser you can retrieve data from ajax as given below:

如果你有app.use(bodyParser.urlencoded({extended:true}));在您的server.js文件中,然后使用bodyParser,您可以从ajax中检索数据,如下所示:

app.post('/', function(req, res) { //your ajax should also post to url: '/'
    var region = req.body.region,
    os = req.body.os;
    // ...
});

#1


1  

If you have app.use(bodyParser.urlencoded({ extended: true })); in your server.js file then using bodyParser you can retrieve data from ajax as given below:

如果你有app.use(bodyParser.urlencoded({extended:true}));在您的server.js文件中,然后使用bodyParser,您可以从ajax中检索数据,如下所示:

app.post('/', function(req, res) { //your ajax should also post to url: '/'
    var region = req.body.region,
    os = req.body.os;
    // ...
});