简体   繁体   English

将参数从javascript传递到python函数?

[英]Passing a parameter to a python function from javascript?

I'm trying to use Parse Cloud Code to run a python script. 我正在尝试使用Parse Cloud Code运行python脚本。 I'm passing a parameter, but I seem to be getting an error. 我正在传递参数,但似乎出现错误。 I'm not 100% sure what the problem is, but it seems like I'm not composing the url correctly. 我不是100%知道问题出在哪里,但是好像我没有正确地组成url。 Any help would be appreciated. 任何帮助,将不胜感激。

My python code looks like this: 我的python代码如下所示:

# a function that makes a sentence more exciting
def excited(sentence):
   return sentence + '!!!'

Here's my code in main.js: 这是我在main.js中的代码:

Parse.Cloud.define('testFunction', function(request, response) {
    var someParam = request.params['testString'];
    var url = 'http://mywebsite.com/test.py/excited&sentence=' + someParam;
    Parse.Cloud.httpRequest({
        url: url,
        success: function(httpResponse) {
            console.log(httpResponse.headers);
            console.log(httpResponse.text);
            response.success();
        }, error: function(httpResponse, error) {
            console.error('Request failed with response code ' + httpResponse.status);
        }
    });
});

EDITED: 编辑:

Now I understand the problem better. 现在,我对问题有了更好的了解。 You are trying to call a python method from Parse.Cloud javascript based service. 您正在尝试从基于Parse.Cloud javascript的服务调用python方法。 Based on their tutorials, I think you probably wanted the other way round. 根据他们的教程,我想您可能想要反过来。

Parse.Cloud allows you to deploy some server-side code in javascript. Parse.Cloud允许您在javascript中部署一些服务器端代码。 Then you can make REST API calls from your mobile app to the server endpoints by using either python or curl or any other language or tool. 然后,您可以使用python或curl或任何其他语言或工具从移动应用程序到服务器端点进行REST API调用。 While testing, you can just call the end points from python on your box. 在测试时,您可以在包装盒上通过python调用端点。

So your server code (in cloud/main.js) should look like this: 因此,您的服务器代码(在cloud / main.js中)应如下所示:

Parse.Cloud.define("testFunction", function(request, response) {
  var excitedSentence = request.params.testString + "!!!";
  response.success(excitedSentence);
});

This code creates a REST API endpoint at https://api.parse.com/1/functions/testFunction 此代码在https://api.parse.com/1/functions/testFunction创建REST API端点

Then you can call this API endpoint by using python (assuming you have Python installed on your box): 然后,您可以使用python调用此API端点(假设您的包装盒中安装了Python):

import json,httplib
connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()
connection.request('POST', '/1/functions/testFunction', json.dumps({
       "testString": "This is an example sentence"
     }), {
       "X-Parse-Application-Id": "xxxxx_PUT_YOUR_ID_xxxxxxx",
       "X-Parse-REST-API-Key": "xxxxxxxx_YOUR_KEY_xxxxxx",
       "Content-Type": "application/json"
     })
result = json.loads(connection.getresponse().read())
print result

If you don't have python installed, you can call the API endpoint by going to the online dashboard. 如果您没有安装python,则可以通过转到在线仪表板来调用API端点。

  1. Go to: Your App -> Core tab -> API Console. 转到:您的应用->核心选项卡-> API控制台。
  2. For endpoint, select post (this is important), and specify "functions/testFunction" in the text box. 对于端点,选择发布(这很重要),然后在文本框中指定“ functions / testFunction”。
  3. In request body, specify: {"testString" : "This is an example sentence"} 在请求正文中,指定:{“ testString”:“这是一个例句”}
  4. Click "Send Request" and you should see the following output: 单击“发送请求”,您应该看到以下输出:

     "result": "This is an example sentence!!!" 

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM