如何在解析云代码和客户端之间正确传递参数? (httpRequest示例) [英] How-to correctly pass params between Parse Cloud Code and the Client? (httpRequest Example)
问题描述
我已经看到了一些示例,阅读了文档并阅读了其他问题,但是我仍然不确定在何处添加/正确添加要在Cloud Code和我的客户端之间正确传递的参数.
I've seen some examples, read the docs and read other question however I'm still not quite sure where to add/properly add params to be passed between Cloud Code and my Client side correctly.
对于实例,这里我是通过我的云代码中的httpRequest创建一个新的类
在我的云代码中main.js
In my Cloud Code main.js
Parse.Cloud.define("POSTfromCloud", function(request, response) {
//runs when Parse.Cloud.run("POSTfromCloud") on the client side is called
Parse.Cloud.httpRequest({
method: "POST",
headers: {
"X-Parse-Application-Id": "[PARSE_APP_ID]",
"X-Parse-REST-API-Key": "[PARSE_REST_ID]",
"Content-Type": "application/json"
},
//adds a new class to my parse data
url: "https://api.parse.com/1/classes/newPOSTfromCloudClass/",
body: {
"newPOSTfromCloudClass": {"key1":"value1","key2":"value2"}
},
success: function (httpResponse) {
console.log(httpResponse.text);
response.success(httpResponse);
},
error:function (httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
response.error(httpResponse.status);
}
}); //end of Parse.Cloud.httpRequest()
});
在我的客户端
Parse.Cloud.run('POSTfromCloud', {}, {
success: function(result) {
console.log("Posted a new Parse Class from Cloud Code Successfully! :"+ JSON.stringify(result))
},
error: function(error) {
console.log("Oops! Couldn't POST from Cloud Code successfully.. :"+ error)
}
});
}
我的结果:
Bam!得到了正确的工作.现在说我想让我的网址成为传递的许多参数之一,我该怎么做呢?
Bam! Got that working correctly. Now lets say I want to make my url one of many parameters passed how do I do this?
推荐答案
当我问这个问题时,我也在修补某些事情,因为我无法正确传递任何东西(否则它将返回为空值),所以这里有一个示例,说明如何将参数传递给它.
As I was asking this I was also tinkering with some things because I coundn't get anything to pass correctly (or it would return as an empty value) so here I have and example on how I can pass parameters into this.
在我的云代码main.js中
Parse.Cloud.define("POSTfromCloud", function(request, response) {
//HERE- make a new instance of 'myValue' for Cloudcode to handle
var myValue = request.params.myValue;
Parse.Cloud.httpRequest({
method: "POST",
....[blah blah]
//AND HERE- placed that here in my body, **note:** you shouldnt store tokens like this, ignore what I named it
body: {
"newPOSTfromCloudClass": {"yourToken":myValue,"key2":"value2"}
},
客户端
var myvalue = "I'm The VALUE";
Parse.Cloud.run('POSTfromCloud', {myValue: myvalue}, {
success: function(result) {
结果:这应该已经正确传递了参数.再次使用标题"yourToken"忽略我,您不应该那样存储令牌.
Result: this should have passed the param correctly. Again ignore me using the title "yourToken", you shouldn't be storing tokens like that.
这花了一段时间,我希望这可以对某人有所帮助.
This took a while to put together, I hope this can help someone.
这篇关于如何在解析云代码和客户端之间正确传递参数? (httpRequest示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!