简体   繁体   English

需要帮助发送数据并在服务器中访问它。 我想用JavaScript(nodejs)做到这一点

[英]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.

相关问题 我想显示一个“加载”微调器,直到数据完全从服务器加载。 如何在 javascript 中为 salesforce lwc 执行此操作? - I want to display a 'loading' spinner until data is loaded completely from the server. How to do this in javascript for salesforce lwc? 使用以下代码向服务器发送jQuery或AngularJS Ajax“ POST”请求,但是不向服务器发送参数数据。 - jQuery or AngularJS Ajax “POST” request to server using code below however do not sending param data to server. JavaScript:需要帮助访问 API JSON 数据 - JavaScript: Need Help accessing API JSON data 反复将PNG发送到服务器。 我想告诉传入的浏览器刷新计时器 - Repeatedly sending PNGs to a server. I want to tell the incoming browser to refresh on a timer 我需要帮助,无需使用表单即可使用Ajax,PHP和mySQL将信息发送到服务器 - I need help sending information to my server using Ajax, PHP, and mySQL without using a form 将数据发送到Node.js服务器 - Sending data to nodejs server 需要帮助让我的页脚做我想做的事情 - Need help getting my footer to do what I want it to do 在使用JavaScript的模拟数据生成器上需要帮助 - Need help on mock data generator using javascript 我需要帮助在 Javascript 中使用双重承诺 - I need help in using double Promises in Javascript 需要使用帮助将表单数据发送到电子邮件地址 - Need help sending form data to an e-mail address using
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM