【问题标题】:TypeError: not indexable类型错误:不可索引
【发布时间】:2019-09-09 20:43:22
【问题描述】:

当尝试通过 ajax 打印作为 x 传入的变量时,它包含从文本字段到我的 python 脚本的元素,它提示我错误 TypeError: not indexable

我尝试将 FieldStorage 值存储在不同的变量中,然后打印:

post = cgi.FieldStorage()
thing = post.getvalue("x")
print thing

但它仍然提示我同样的错误

这是我的 ajax 代码:

$.ajax({
    type: 'POST',
    url: 'test.py',
    cache: false,
    contentType:"application/text-plain; charset=utf-8",
    data: {
        "x": $("input#name").val()
    },
    success: function(data) {
        $("p#second").text(data);

    },
    error: function(data){
        $("p#second").text("ya sorry nah");
    }

});

确切的错误:

Traceback (most recent call last): File "/home/main/code/test.py", line 12, in <module> thing = post.getvalue("x") File "/usr/lib/python2.7/cgi.py", line 548, in getvalue if key in self: File "/usr/lib/python2.7/cgi.py", line 594, in __contains__ raise TypeError, "not indexable" TypeError: not indexable -->

【问题讨论】:

  • @Carcigenicate 我已经添加了确切的错误
  • 为什么要将内容类型设置为纯文本?你为什么要设置它? (你为什么使用 cgi,但这是一个单独的问题。)
  • @DanielRoseman 我之前没有使用过 ajax,所以查看其他人的脚本我认为这是必要的,但现在我把它拿出来并且它可以工作。谢谢

标签: python ajax cgi


【解决方案1】:

正如@DanielRoseman 所说,您的ajax 代码中不需要contentType

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-16
    • 2023-01-20
    • 2017-01-01
    • 2021-03-23
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 2018-10-15
    相关资源
    最近更新 更多