【问题标题】:What's wrong with my format of passing data using $.getJSON?我使用 $.getJSON 传递数据的格式有什么问题?
【发布时间】:2017-02-25 17:30:43
【问题描述】:

请我再借一双眼睛看看我的 Ajax 请求格式有什么问题。

首先我将参数作为对象传递,如下所示:

$.getJSON('selectOne', {'modelClass': 'Address', 'id': '3'}, function(json, textStatus) {
  $('span#address_line').append(json);
});

不是将值'Address' 发送到后端,而是密钥'modelClass' 实际上是我的服务器得到的。与'id' 而不是'3' 的情况相同。

然后我把参数部分改成数组,像这样:

    $.getJSON('selectOne', ['Address', '3'], function(json, textStatus) {
      $('span#address_line').append(json);
    });

现在两个参数都以undefined 发送。

它永远不会起作用。这里有什么问题?

我的后端服务器是一个名为cherrypy的python框架。

编辑:在这里我粘贴服务器对第一种情况的抱怨:

    500 Internal Server Error

    The server encountered an unexpected condition which prevented it from fulfilling the request.

    Traceback (most recent call last):
      File "/usr/local/lib/python3.4/dist-packages/cherrypy/_cprequest.py", line 670, in respond
... ...
      File "/home/njh/gui_Base.py", line 30, in selectOne
        data_record = db.query(modelClass).filter(modelClass.id == id).one()
    AttributeError: 'str' object has no attribute 'id'

【问题讨论】:

  • 在您的第一个示例中,您打开一个对象{,但关闭一个数组]。这是您的问题还是您的真实代码中的错字?
  • 第一个看起来像语法错误,因为它一端是对象,另一端是数组?你应该向我们展示你是如何在服务器上捕获这个的。
  • @Turnip,感谢您的注意,这是一个错字。我已经更正了。
  • @adeneo,感谢您的提醒。我现在粘贴服务器响应。
  • @JinghuiNiu 你应该先JSON.stringify() 传递给$.getJSON() 的对象。

标签: jquery getjson cherrypy ajaxform


【解决方案1】:

感谢 webKnjaZ 的评论,使用JSON.stringify 是解决此类故障的正确方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    相关资源
    最近更新 更多