[英]Need help sending data and accessing it in the server. I want to do this using JavaScript(nodejs)
I've been having trouble with front-end back-end interactions. 我在前端后端交互方面遇到了麻烦。 I'm relatively sure I'm sending the data but I cannot access the data afterwards. 我相对确定我要发送数据,但之后无法访问数据。 Most recently I have used this code template (from mozilla's help pages a link ) to send the data. 最近,我使用了此代码模板(来自mozilla的帮助页面的链接 )来发送数据。 JavaScript: JavaScript:
function sendData(data) {
var XHR = new XMLHttpRequest();
var urlEncodedData = "";
// We turn the data object into a URL encoded string
for(name in data) {
urlEncodedData += name + "=" + data[name] + "&";
}
// We remove the last "&" character
urlEncodedData = urlEncodedData.slice(0, -1);
// We URLEncode the string
urlEncodedData = encodeURIComponent(urlEncodedData);
// encodeURIComponent encode a little to much things
// to properly handle HTTP POST requests.
urlEncodedData = urlEncodedData.replace('%20','+').replace('%3D','=');
// We define what will happen if the data are successfully sent
XHR.addEventListener('load', function(event) {
alert('Yeah! Data sent and response loaded.');
});
// We define what will happen in case of error
XHR.addEventListener('error', function(event) {
alert('Oups! Something goes wrong.');
});
// We setup our request
XHR.open('POST', 'http://ucommbieber.unl.edu/CORS/cors.php');
// We add the required HTTP header to handle a form data POST request
XHR.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
XHR.setRequestHeader('Content-Length', urlEncodedData.length);
// And finally, We send our data.
XHR.send(urlEncodedData);
}
HTML: HTML:
<button type="button" onclick="sendData({test:'ok'})">Click Me!</button>
My questions are: is there a better way of sending data (more suited to node)? 我的问题是:是否有更好的发送数据的方式(更适合于节点)? and how can I access the data on the server side? 以及如何访问服务器端的数据?
is there a better way of sending data? 有没有更好的数据发送方式?
This is rather subjective question, but here is an alternative: you can create form with hidden elements and send them to the server using FormData()
. 这是一个相当主观的问题,但是这是一个替代方案:您可以创建带有隐藏元素的表单,然后使用FormData()
将它们发送到服务器。 This allows you also comfortable files processing: 这还使您可以轻松地处理文件:
<form onsubmit="xhrsend(event,this)" method="POST" enctype="multipart/form-data" action="http://ucommbieber.unl.edu/CORS/cors.php">
<input type="hidden" name="myName" value="myValue"/>
<input type="file" name="myFile"/>
<input type="submit" value="Send"/>
...
</form>
use universal JS to XHR send any form 使用通用JS来XHR发送任何形式
function xhrsend(ev,frm) {
ev.preventDefault(); // prevent submiting form
var XHR = new XMLHttpRequest();
XHR.addEventListener(...); // whatever
XHR.open('POST', frm.action, true);
XHR.send(new FormData(frm)); // send form data
this.reset(); // optional: reset form values
}
how can I access the data on the server side? 如何访问服务器端的数据?
This question will guide you how to handle POST data on node.js server. 这个问题将指导您如何在node.js服务器上处理POST数据。
Note: if you play with node.js, I recommend to have a look at websockets - it can do more than XHR (like sending message from server to client). 注意:如果您使用node.js,我建议您看一下websockets-它可以做的比XHR还要多(例如,从服务器向客户端发送消息)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.