久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<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="qwawimqqmiuu" 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. 為什么發(fā)送到 Node/Express 服務(wù)器的 XMLHttpRequest 中

        Why is an object in an XMLHttpRequest sent to a Node/Express server empty?(為什么發(fā)送到 Node/Express 服務(wù)器的 XMLHttpRequest 中的對(duì)象是空的?)
        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="qwawimqqmiuu" 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. 本文介紹了為什么發(fā)送到 Node/Express 服務(wù)器的 XMLHttpRequest 中的對(duì)象是空的?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  我正在嘗試制作一個(gè)表格,該表格采用電子郵件地址并將交易電子郵件發(fā)回.我在 vanilla JavaScript 中使用 XMLHttpRequest 向服務(wù)器發(fā)送數(shù)據(jù),但是當(dāng)我查看從 index.html 發(fā)送的數(shù)據(jù)時(shí),它只是服務(wù)器端的一個(gè)空對(duì)象.

                  在后端,我使用的是 Node、Express 和 Nodemailer.Nodemailer 工作正常.我一直在試圖弄清楚為什么查詢(xún)對(duì)象中沒(méi)有任何內(nèi)容.

                  //這里是 server.jsvar express = require('express');var nodemailer = require('nodemailer');var app = express();//發(fā)送 index.htmlapp.get('/', function(request, response) {response.sendfile('index.html');});//我應(yīng)該從用 index.html 編寫(xiě)的 JS 接收數(shù)據(jù)的地方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="寫(xiě)點(diǎn)東西"></textarea><button id="submit">提交</button></div><腳本>//當(dāng)#submit 被點(diǎn)擊時(shí),它會(huì)調(diào)用一個(gè)函數(shù)來(lái)收集值,然后像下面這樣發(fā)出一個(gè) XMLHttpRequestdata = {to: to, subject: subject, text: text};var request = new XMLHttpRequest();request.open('GET', 'http://localhost:3000/send', true);請(qǐng)求.發(fā)送(數(shù)據(jù));}</script>

                  解決方案

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

                  • 決定是使用 GET 還是 POST,您似乎對(duì)使用哪一個(gè)感到困惑.我會(huì)使用 POST,因?yàn)槟趪L試為電子郵件發(fā)送數(shù)據(jù),而不是真正嘗試從服務(wù)器獲取數(shù)據(jù).
                  • 更改您的 app.post 節(jié)點(diǎn)功能(假設(shè)您想要發(fā)布)
                  • 您需要向服務(wù)器發(fā)送一個(gè)字符串,因此 json 字符串化
                  • 由于您的字符串是 json 格式,您需要將標(biāo)題Content-Type"更改為application/json"
                  • 您需要將請(qǐng)求動(dòng)詞更改為POST"以匹配您的服務(wù)器以及您要完成的任務(wù)

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

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

                  這應(yīng)該可以在客戶(hù)端上解決問(wèn)題

                  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 的替代解決方案

                  或者,您可以通過(guò) HTTP api 查看這個(gè)糖庫(kù) - axios

                  如果你用的是axios,那么簡(jiǎn)單

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

                  或者如果您想控制收到回復(fù)時(shí)發(fā)生的情況.

                  data = {to: to, subject: subject, text: text};axios.post('/user', 數(shù)據(jù)).then(函數(shù)(響應(yīng)){console.log('成功');}).catch(函數(shù)(響應(yīng)){console.log('錯(cuò)誤');});

                  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');
                    });
                  

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

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

                  相關(guān)文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調(diào)用 abort (jQuery) 之后,瀏覽器也會(huì)等待 ajax 調(diào)用完成)
                  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 無(wú)法加載,請(qǐng)求的資源上不存在“Access-Control-Allow-Origin標(biāo)頭) - IT屋-程序員軟件開(kāi)發(fā)技術(shù)分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請(qǐng)求是否有可能不遵循重定向 (301 302))
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內(nèi)容)
                  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="qwawimqqmiuu" 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'>

                          • 主站蜘蛛池模板: 91在线视频免费观看 | 欧美久久天堂 | 日日摸日日碰夜夜爽2015电影 | 日韩精品在线观看网站 | 一级黄在线观看 | 国产成人精品午夜 | 久久精品国产免费高清 | 亚洲人人 | 亚洲h视频| 中文在线一区二区 | 精品一区在线 | 成人免费视频网站在线观看 | 91精品www| 黄色一级片aaa| 亚洲啪啪| 91久久精 | 91看片免费 | 一级片毛片| 丁香婷婷久久久综合精品国产 | 日本aa毛片a级毛片免费观看 | aaa大片免费观看 | 在线看无码的免费网站 | 久久激情网 | 日韩色图视频 | 久草免费视 | 国产精品成人一区二区三区 | 中文字幕av中文字幕 | 欧美性生交大片免费 | 龙珠z在线观看 | 久久久123 | 国产福利91精品一区二区三区 | 久久网一区二区 | 亚洲一区视频在线 | 婷婷久 | 成人免费在线视频 | 久久综合久色欧美综合狠狠 | 欧美精品成人 | 做a视频 | 伊人久久精品 | 999在线精品 | 欧美aaa|