亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Meteor 中將數據從客戶端發送到服務器?

如何在 Meteor 中將數據從客戶端發送到服務器?

牧羊人nacy 2023-05-25 18:13:22
我正在開發一個應用程序,但遇到了一個問題。我在“./public”文件夾中有 HTML 頁面。在“./public/js”中,我有一個腳本,可以在填寫此頁面上的表格后收集一些數據。然后我需要將這些數據發送到服務器,服務器將使用這些數據進行一些計算。在該服務器應該發回數據之后,我可以將其顯示在 HTML 結果頁面上。我的想法是我需要對客戶端隱藏此代碼。有什么想法可以做到嗎?編輯:知道如何實現它。在 server/main.js 中我有 WebApp.connectHandlers。我也使用 connect-route 包。所以我需要在 public/example.js 中創建 post xhr 并將值放入其中。url 應該與 router.get('/someurl', .....) 中的 '/someurl' 相同,對嗎?如何正確完成?這是我現在擁有的來自 server/main.js 的一些代碼:    WebApp.connectHandlers.use(connectRoute(function (router) {      router.get('/example', staticFile(process.cwd() + '/../web.browser/app' + 'example.html'))問題是我從 example.html 中的表單中獲取了一些值,其中 .js 文件存儲在 /public 中。然后我創建 xhr post 請求并指示應該作為 server/main.js 中的 router.get() 中的第一個 arg 的 url。/public/example.js 代碼片段:    const values = document.querySelector("input").value  //example of some data from form    const xhr = new XHRHttpRequest()    xhr.open('POST', '/someurl')    xhr.send(values)現在我需要在 server/main.js 中獲取這個請求,但我不能在一個 url 上使用 router.get('/example',.....) 兩次。我的意思是它不會像這樣工作:    WebApp.connectHandlers.use(connectRoute(function (router) {      router.get('/example', staticFile(process.cwd() + '/../web.browser/app' + 'example.html'))      router.get('/example', (req, res, next) {...});可能我對它的看法不對,但還沒有發現它是如何工作的。那我現在能做什么?
查看完整描述

2 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

https://docs.meteor.com/api/methods.html


Meteor.call("yourMethodName", (err, result) => {

  // Put your code to update HTML with result

});

如果您想使用 Meteor.call,將您的 html 放在公用文件夾中也可能是個壞主意。


查看完整回答
反對 回復 2023-05-25
?
SMILET

TA貢獻1796條經驗 獲得超4個贊

我已經解決了這個問題并解決了它。在 server/main.js 我添加這段代碼:


router.post('/method/example', (req, res, next) => {

    let data = '';

    req.on("data", chunk => {

      data += chunk;

    });

    req.on('end', () => {

      const result = dataHandler(data);

      res.write(`${result}`);

      res.end();

    });

}

在 /public/example.js 中,我剛剛使用與 server/mains.js 中新行中相同的 URL 執行了一個 xhr post 請求。它是這樣的:


const xhr = new XMLHttpRequest();

xhr.open('post', '/method/example');

xhr.send(data);

xhr.addEventListener("load", () => {

    const reqResult = xhr.responseText;

}


查看完整回答
反對 回復 2023-05-25
  • 2 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號