[英]Sending data to Firebase Cloud Function via Ajax
I am trying to send data through post OR get to firebase cloud function but couldn't get it work. 我正在尝试通过发布发送数据,或者进入Firebase云功能,但是无法正常工作。
Here is my ajax : 这是我的ajax :
$.ajax({
url: 'https://us-ceXXX1-reXXXts-fXXa-pr.cloudfunctions.net/helloWorld',
dataType: "json",
method: 'GET',
crossDomain: true,
body: {
mobileNo: "WzkyMzXXXXXXXXXXXc5ODBd"
},
success: function(data){
console.log('succes: '+data);
}
});
And Here Is Cloud Function: 这是云功能:
exports.helloWorld = functions.https.onRequest((request, response) => {
var responsez = response;
console.log("DATA IS:"+request.data); //prints undefined
console.log("BODY IS:"+JSON.stringify(request.body)); //prints BODY ID: {}
var mobNo = request.body.mobileNo;
var options = {
body: {mobileNo: mobNo, applicationVersion: "1.0"},
url: 'http://nxxxpi.fixxxa.wxb.pk/GetxxxxxxoUxr',
headers: {
'appKey':'jDxxxxxxxOr',
'appId':'2xxxxx9'
},
json: true
}
cors(request, response, () => {requestz.post(options, function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
responsez.send(JSON.stringify(body));
})});
});
And It console logs. 并且它控制台日志。
DATA IS: undefined
数据是:未定义
BODY IS: {}
身体是:{}
EDIT: HERE IS THE FIREBASE CONSOLE: 编辑:这里是FIREBASE控制台:
After adding: 添加后:
console.log("BODY MOBILE:"+JSON.stringify(request.body.mobileNo));
console.log("ONLY MOBILE:"+JSON.stringify(request.mobileNo));
There are few issues in your code 您的代码中几乎没有问题
change the method to POST
instead of GET
in your ajax call as you are passing the data in request body 当您在请求正文中传递数据时,在ajax调用中将方法更改为
POST
而不是GET
use the data
property instead of body
in the ajax call . 在ajax调用中使用
data
属性而不是body
。 it should work now. 它现在应该可以工作。 The data will be available in
request.body
inside the firebase function 数据将在firebase函数内的
request.body
可用
$.ajax({ url: 'https://us-ceXXX1-reXXXts-fXXa-pr.cloudfunctions.net/helloWorld', dataType: "json", method: 'POST', crossDomain: true, data: { mobileNo: "WzkyMzXXXXXXXXXXXc5ODBd" }, success: function(data){ console.log('succes: '+data); } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.