pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<tfoot id='kEFGf'></tfoot>

      <legend id='kEFGf'><style id='kEFGf'><dir id='kEFGf'><q id='kEFGf'></q></dir></style></legend>
      <i id='kEFGf'><tr id='kEFGf'><dt id='kEFGf'><q id='kEFGf'><span id='kEFGf'><b id='kEFGf'><form id='kEFGf'><ins id='kEFGf'></ins><ul id='kEFGf'></ul><sub id='kEFGf'></sub></form><legend id='kEFGf'></legend><bdo id='kEFGf'><pre id='kEFGf'><center id='kEFGf'></center></pre></bdo></b><th id='kEFGf'></th></span></q></dt></tr></i><div class="0yq2aeq" id='kEFGf'><tfoot id='kEFGf'></tfoot><dl id='kEFGf'><fieldset id='kEFGf'></fieldset></dl></div>

        <small id='kEFGf'></small><noframes id='kEFGf'>

          <bdo id='kEFGf'></bdo><ul id='kEFGf'></ul>
      1. 為什么發送到 Node/Express 服務器的 XMLHttpRequest 中

        Why is an object in an XMLHttpRequest sent to a Node/Express server empty?(為什么發送到 Node/Express 服務器的 XMLHttpRequest 中的對象是空的?)
        1. <tfoot id='tZeYx'></tfoot>
          • <small id='tZeYx'></small><noframes id='tZeYx'>

              <i id='tZeYx'><tr id='tZeYx'><dt id='tZeYx'><q id='tZeYx'><span id='tZeYx'><b id='tZeYx'><form id='tZeYx'><ins id='tZeYx'></ins><ul id='tZeYx'></ul><sub id='tZeYx'></sub></form><legend id='tZeYx'></legend><bdo id='tZeYx'><pre id='tZeYx'><center id='tZeYx'></center></pre></bdo></b><th id='tZeYx'></th></span></q></dt></tr></i><div class="wuaqoyk" id='tZeYx'><tfoot id='tZeYx'></tfoot><dl id='tZeYx'><fieldset id='tZeYx'></fieldset></dl></div>
              <legend id='tZeYx'><style id='tZeYx'><dir id='tZeYx'><q id='tZeYx'></q></dir></style></legend>
                <bdo id='tZeYx'></bdo><ul id='tZeYx'></ul>

                    <tbody id='tZeYx'></tbody>

                1. 本文介紹了為什么發送到 Node/Express 服務器的 XMLHttpRequest 中的對象是空的?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試制作一個表格,該表格采用電子郵件地址并將交易電子郵件發回.我在 vanilla JavaScript 中使用 XMLHttpRequest 向服務器發送數據,但是當我查看從 index.html 發送的數據時,它只是服務器端的一個空對象.

                  在后端,我使用的是 Node、Express 和 Nodemailer.Nodemailer 工作正常.我一直在試圖弄清楚為什么查詢對象中沒有任何內容.

                  //這里是 server.jsvar express = require('express');var nodemailer = require('nodemailer');var app = express();//發送 index.htmlapp.get('/', function(request, response) {response.sendfile('index.html');});//我應該從用 index.html 編寫的 JS 接收數據的地方app.post('/send', function(req, res) {var mailOptions = {到:req.query.to,主題:req.query.subject,文本:req.query.text}});

                  <!-- 這是我的 index.html,里面有一些 JS -->

                  <input id="to" type="text" placeholder="Email"/><input id="subject" type="text" placeholder="subject"/><textarea id="content" cols="20" rows="2" placeholder="寫點東西"></textarea><button id="submit">提交</button></div><腳本>//當#submit 被點擊時,它會調用一個函數來收集值,然后像下面這樣發出一個 XMLHttpRequestdata = {to: to, subject: subject, text: text};var request = new XMLHttpRequest();request.open('GET', 'http://localhost:3000/send', true);請求.發送(數據);}</script>

                  解決方案

                  在此之前的一些事情可以工作

                  • 決定是使用 GET 還是 POST,您似乎對使用哪一個感到困惑.我會使用 POST,因為您正在嘗試為電子郵件發送數據,而不是真正嘗試從服務器獲取數據.
                  • 更改您的 app.post 節點功能(假設您想要發布)
                  • 您需要向服務器發送一個字符串,因此 json 字符串化
                  • 由于您的字符串是 json 格式,您需要將標題Content-Type"更改為application/json"
                  • 您需要將請求動詞更改為POST"以匹配您的服務器以及您要完成的任務

                  在您的服務器中,您需要將 app.post 代碼替換為(您需要 npm install body-parser)

                  var bodyParser = require('body-parser');app.use(bodyParser.json());//用于解析應用程序/jsonapp.use(bodyParser.urlencoded({extended: true }));//用于解析應用程序/x-www-form-urlencoded//我應該從用 index.html 編寫的 JS 接收數據的地方app.post('/send', function(req, res) {var mailOptions = {到:req.body.to,主題:req.body.subject,文本:req.body.text}});

                  這應該可以在客戶端上解決問題

                  data = {to: to, subject: subject, text: text};var request = new XMLHttpRequest();request.open('POST', 'http://localhost:3000/send', true);xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");request.send(JSON.stringify(data));

                  XMLHttpRequest 的替代解決方案

                  或者,您可以通過 HTTP api 查看這個糖庫 - axios

                  如果你用的是axios,那么簡單

                  data = {to: to, subject: subject, text: text};axios.post('/user', 數據);

                  或者如果您想控制收到回復時發生的情況.

                  data = {to: to, subject: subject, text: text};axios.post('/user', 數據).then(函數(響應){console.log('成功');}).catch(函數(響應){console.log('錯誤');});

                  I am trying to make a form that takes the email address and sends a transactional email back. I am using a XMLHttpRequest in vanilla JavaScript to send data to the server, but when I look at the data sent from index.html, it is only an empty object on the server side.

                  On the backend I am using Node and Express and Nodemailer. Nodemailer is working properly. I have been trying to figure out why the query object does not have anything in it.

                  // Here is server.js
                  
                  var express = require('express');
                  var nodemailer = require('nodemailer');
                  var app = express();
                  
                  // Send index.html
                  app.get('/', function(request, response) {
                    response.sendfile('index.html');
                  });
                  
                  // Where I should receive data from JS written in index.html
                  app.post('/send', function(req, res) {
                    var mailOptions  =   {
                      to: req.query.to,
                      subject: req.query.subject,
                      text: req.query.text
                    }
                  });

                  <!-- Here is my index.html with some JS in it -->
                  
                  <div>
                    <input id="to" type="text" placeholder="Email" />
                    <input id="subject" type="text" placeholder="subject" />
                    <textarea id="content" cols="20" rows="2" placeholder="Write something"></textarea>
                    <button id="submit">Submit</button>
                  </div>
                  
                  <script>
                    // When #submit is clicked it invokes a function to collect values and then makes a XMLHttpRequest like bellow
                    data = {to: to, subject: subject, text: text};
                    var request = new XMLHttpRequest();
                    request.open('GET', 'http://localhost:3000/send', true);
                    request.send(data);
                    }
                  </script>

                  解決方案

                  A few things before this can work

                  • Decide whether you want to use GET or POST, you seem to be confused as to which one to use. I would use POST because you're trying to send data for an email and not really trying to get data from the server.
                  • Change your app.post node function (assuming you want post)
                  • You need to send a string to the server, hence the json stringify
                  • Since your string is in json format you need to change the header "Content-Type" to "application/json"
                  • You need to change your request verb to 'POST' to match your server and what you are trying to accomplish

                  In your server you need to replace the app.post code with (you'll need to npm install body-parser)

                  var bodyParser = require('body-parser');
                  app.use(bodyParser.json()); // for parsing application/json
                  app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
                  // Where I should receive data from JS written in index.html
                  app.post('/send', function(req, res) {
                    var mailOptions  =   {
                      to: req.body.to,
                      subject: req.body.subject,
                      text: req.body.text
                    }
                  });
                  

                  This should do the trick on the client

                  data = {to: to, subject: subject, text: text};
                  var request = new XMLHttpRequest();
                  request.open('POST', 'http://localhost:3000/send', true);
                  xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
                  request.send(JSON.stringify(data));
                  

                  Alternative Solution to XMLHttpRequest

                  Alternatively, you can look at this library for sugar over the HTTP api - axios

                  If you're using axios, it's as simple as

                  data = {to: to, subject: subject, text: text};
                  axios.post('/user', data);
                  

                  or if you want to control what happens when you receive a response.

                  data = {to: to, subject: subject, text: text};
                  axios.post('/user', data)
                    .then(function (response) {
                      console.log('success');
                    })
                    .catch(function (response) {
                      console.log('error');
                    });
                  

                  這篇關于為什么發送到 Node/Express 服務器的 XMLHttpRequest 中的對象是空的?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標頭) - IT屋-程序員軟件開發技術分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內容)
                  Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)
                    <tbody id='4Wgw0'></tbody>

                    • <bdo id='4Wgw0'></bdo><ul id='4Wgw0'></ul>
                    • <tfoot id='4Wgw0'></tfoot>
                      <i id='4Wgw0'><tr id='4Wgw0'><dt id='4Wgw0'><q id='4Wgw0'><span id='4Wgw0'><b id='4Wgw0'><form id='4Wgw0'><ins id='4Wgw0'></ins><ul id='4Wgw0'></ul><sub id='4Wgw0'></sub></form><legend id='4Wgw0'></legend><bdo id='4Wgw0'><pre id='4Wgw0'><center id='4Wgw0'></center></pre></bdo></b><th id='4Wgw0'></th></span></q></dt></tr></i><div class="0qyeeae" id='4Wgw0'><tfoot id='4Wgw0'></tfoot><dl id='4Wgw0'><fieldset id='4Wgw0'></fieldset></dl></div>

                    • <legend id='4Wgw0'><style id='4Wgw0'><dir id='4Wgw0'><q id='4Wgw0'></q></dir></style></legend>

                          <small id='4Wgw0'></small><noframes id='4Wgw0'>

                          • 主站蜘蛛池模板: 气密性检测仪_气密性检测设备_防水测试仪_密封测试仪-岳信仪器 | 骨密度检测仪_骨密度分析仪_骨密度仪_动脉硬化检测仪专业生产厂家【品源医疗】 | 企业VI设计_LOGO设计公司_品牌商标设计_【北京美研】 | 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 | SRRC认证|CCC认证|CTA申请_IMEI|MAC地址注册-英利检测 | EPK超声波测厚仪,德国EPK测厚仪维修-上海树信仪器仪表有限公司 | 本安接线盒-本安电路用接线盒-本安分线盒-矿用电话接线盒-JHH生产厂家-宁波龙亿电子科技有限公司 | 【黄页88网】-B2B电子商务平台,b2b平台免费发布信息网 | 大数据营销公司_舆情监测软件_上海SEO公司-文军营销官网 | 干式变压器厂_干式变压器厂家_scb11/scb13/scb10/scb14/scb18干式变压器生产厂家-山东科锐变压器有限公司 | 浙江建筑资质代办_二级房建_市政_电力_安许_劳务资质办理公司 | 工业胀紧套_万向节联轴器_链条-规格齐全-型号选购-非标订做-厂家批发价格-上海乙谛精密机械有限公司 | 地图标注|微信高德百度地图标注|地图标记-做地图[ZuoMap.com] | 深圳昂为官网-气体分析仪,沼气分析仪,动态配气仪,气体传感器厂家 | 除湿机|工业除湿机|抽湿器|大型地下室车间仓库吊顶防爆除湿机|抽湿烘干房|新风除湿机|调温/降温除湿机|恒温恒湿机|加湿机-杭州川田电器有限公司 | 稳尚教育加盟-打造高考志愿填报平台_新高考志愿填报加盟_学业生涯规划加盟 | 仓储货架_南京货架_钢制托盘_仓储笼_隔离网_环球零件盒_诺力液压车_货架-南京一品仓储设备制造公司 | 蓝米云-专注于高性价比香港/美国VPS云服务器及海外公益型免费虚拟主机 | 数控车床-立式加工中心-多功能机床-小型车床-山东临沂金星机床有限公司 | 正压密封性测试仪-静态发色仪-导丝头柔软性测试仪-济南恒品机电技术有限公司 | 真空上料机(一种真空输送机)-百科 | 食品质构分析仪-氧化诱导分析仪-瞬态法导热系数仪|热冰百科 | CE认证_产品欧盟ROHS-REACH检测机构-商通检测 | 中国产业发展研究网 - 提供行业研究报告 可行性研究报告 投资咨询 市场调研服务 | 安徽华耐泵阀有限公司-官方网站 安德建奇火花机-阿奇夏米尔慢走丝|高维|发那科-北京杰森柏汇 | 广西正涛环保工程有限公司【官网】 | 济南画室培训-美术高考培训-山东艺霖艺术培训画室 | 电抗器-能曼电气-电抗器专业制造商 | 飞利浦LED体育场灯具-吸顶式油站灯-飞利浦LED罩棚灯-佛山嘉耀照明有限公司 | 低噪声电流前置放大器-SR570电流前置放大器-深圳市嘉士达精密仪器有限公司 | 电子元器件呆滞料_元器件临期库存清仓尾料_尾料优选现货采购处理交易商城 | 柴油发电机组_柴油发电机_发电机组价格-江苏凯晨电力设备有限公司 | 企典软件一站式企业管理平台,可私有、本地化部署!在线CRM客户关系管理系统|移动办公OA管理系统|HR人事管理系统|人力 | 成都思迪机电技术研究所-四川成都思迪编码器 | 成都租车_成都租车公司_成都租车网_众行宝 | 防火门|抗爆门|超大门|医疗门|隔声门-上海加汇门业生产厂家 | 硬齿面减速机_厂家-山东安吉富传动设备股份有限公司 | 四川实木门_成都实木门 - 蓬溪聚成门业有限公司 | 特种电缆厂家-硅橡胶耐高温电缆-耐低温补偿导线-安徽万邦特种电缆有限公司 | LED投光灯-工矿灯-led路灯头-工业灯具 - 山东普瑞斯照明科技有限公司 | 高速混合机_锂电混合机_VC高效混合机-无锡鑫海干燥粉体设备有限公司 |