我用ajax发送FormData数据:
window.onload = function(){
document.getElementById("sub").addEventListener("click",function(e){
var myFormData = new FormData(document.getElementById("myform"));
var xhr = new XMLHttpRequest();
xhr.open("post","http://localhost:8008/");
xhr.onload = function(){
if(xhr.status === 200){
console.log('请求成功');
}
}
xhr.send(myFormData);
},false);
}
nodejs写的服务器(部分代码):
req.on('data',function(chunk){
data += chunk;
});
req.on("end",function(){
console.log(data);
})
发现data最终是
------WebKitFormBoundaryT7SKm0RPi5FrxsAN
Content-Disposition: form-data; name="name"
小明
------WebKitFormBoundaryT7SKm0RPi5FrxsAN
Content-Disposition: form-data; name="password"
ffff
------WebKitFormBoundaryT7SKm0RPi5FrxsAN--
请问应该如何解析出里面的name和password呢
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
可以使用multiparty来解析form-data的数据
data += chunk获得这个
data就是 post 提交的数据可以用公共模块
querystring进行解析const postData = querystring.parse(data)postData 就是接收到的数据对象