nodejs代碼:
//安裝依賴:npm install body-parser express --save-dev
var express = require("express");
var bodyParser = require('body-parser');//解析,用req.body獲取post參數(shù)
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.post("/",function(req,res){
console.log(JSON.stringify(req.body));
res.send({hello:'world'});
})
請求接口:
//jq
$.ajax({
url:"http://192.168.1.200",
method:"post",
data:{
a:1,
b:2
}
}).done(function(d){
console.log(d)
})
//原生js
var xml=new XMLHttpRequest();
xml.open("POST","http://192.168.1.200",true)
xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xml.send("a=1&b=2");
xml.onreadystatechange=function(){
if (xml.readyState==4&&xml.status==200) {
console.log(xml.responseText);
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者