如何正确地从客户端进行Parse.com Parse.Cloud.httpRequest?

时间:2015-11-07 23:38:27

标签: parse-platform cloud-code http-request

我想从我的客户端调用我的cloudcode发出一个http请求。

1 个答案:

答案 0 :(得分:5)

我一开始觉得这有点令人困惑,所以希望这会有所帮助。

在您的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来调用您放置在云中的匹配Parse.Cloud.define即可。你使用

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

您的结果:假设您的帖子 enter image description here

(这里如果你想要删除这个新对象,你的url会像/newPOSTfromCloudClass/60j1uyaBt那样附加对象id)

知道它不必是httpRequst云功能。您可以在definerun函数中执行“任何操作”。

注意:还看到了关于在此here

中传递参数的其他相关问题