2017-03-28 43 views
0

這是在雲計算作爲一個函數的函數:我如何添加JSON到URL的末尾我怎麼在URL的末尾添加JSON傳遞給函數

/** 
* Responds to any HTTP request that can provide a "message" field in the body. 
* 
* @param {!Object} req Cloud Function request context. 
* @param {!Object} res Cloud Function response context. 
*/ 
exports.helloWorld = function helloWorld(req, res) { 
    // Example input: {"message": "Hello!"} 
    if (req.body.message === undefined) { 
    // This is an error case, as "message" is required. 
     res.status(400).send('No message defined!'); 
    } else { 
    // Everything is okay. 
     console.log(req.body.message); 
     res.status(200).send('Success: ' + req.body.message); 
    } 
}; 

將消息傳遞給函數... 該函數的網址是 https://us-central1-internal-156105.cloudfunctions.net/myapp

+1

你應該解釋多一點點你正在嘗試做的,如果y您希望代碼將您正在發送的消息返回給您,您希望將其發送到消息正文中。這可以通過[cURL]完成(https://curl.haxx.se/docs/httpscripting.html)。根據您使用的語言,您可以通過很多方法獲得相同的結果。查看[AJAX請求](https://www.w3schools.com/xml/ajax_intro.asp),這是最常見的一種。 – Nicolas

+0

我認爲可能有一種方法可以通過瀏覽器在URL末尾發送消息,如https://us-central1-internal-156105.cloudfunctions.net/myapp?JSON=[{"message「:」 Hello World「}] ...我明顯是新手......請原諒我缺乏協議和知識 –

+0

您是否熟悉URL的語法?如果沒有,https://en.wikipedia.org/wiki/URL提供了URL的可能性的概述,順便說一下,它基本上與JSON不兼容。 –

回答

0

使用「結束」

res.end(req.body.message)