javascript - 使用 axios 向cherrypy 函数发送 POST 请求时输入被忽略

标签 javascript python http cherrypy axios

所以我不确定代码的哪一边是错误的,javascript还是python。

基本上我有reactjs表单,然后将其提交到服务器并通过cherrypy函数进行处理。据称,请求本身正在处理,但他们没有收到我发送的任何数据。

以下是其中一个请求和 python 函数的示例:

 handleSubmit(e){
      var example = this.props.value;
      const request = axios.post(someUrl+'/pythonFunction', {example})
      .then(function(response){
      console.log('successfully posted', example);
      })

    }

现在这是正在执行此操作的函数的 Python 代码:

@cherrypy.tools.allow(methods='POST')
@cherrypy.tools.json_out()
def pythonFunction(self, **kwargs):
    # does stuff with data received

所以,问题是 python 端没有收到任何信息。无论我如何双重和三重检查以确保事物正确发送,诸如 kwargs 之类的参数将始终为空。

关于问题实际出在哪里以及如何解决它有什么想法吗?

编辑:不确定是否相关,但我尝试发布的数据确实出现在请求负载上

最佳答案

以下是如何读取 JSON 格式的有效负载(通过任何 HTTP 方法提交,其中包括正文):

@cherrypy.tools.allow(methods='POST')
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def pythonFunction(self, *args **kwargs):
    data = cherrypy.request.json
    return data

关于javascript - 使用 axios 向cherrypy 函数发送 POST 请求时输入被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41382033/

相关文章:

http - 在 Cache-Control HTTP header 中包含 max-age 和 s-maxage 有意义吗?

PHP - HTTP header 中的文件名 : Problem with whitespaces

javascript - React InstantSearch RefinementList transformItems 不工作

javascript - 如何设置整个按钮组的宽度?

javascript - 如何使用jquery点击按钮隐藏数据库记录

Python:从文本中拆分数字然后求和

python - Django 的 HTTP 响应代码后面的数字

python - sqlalchemy.exc.NoReferencedTableError : Foreign key associated with column X could not find table Y with which to generate a foreign key 错误

javascript - instanceof - Stoyan Stefanov 的面向对象 javascript 中的 javascript 不一致

http - Angular 2 HTTP GET 返回 URL null