TypeError:不可索引

时间:2019-04-19 15:29:40

标签: python ajax cgi

当尝试打印以x形式传入的变量时,该变量包含通过ajax从文本字段传递到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 -->

1 个答案:

答案 0 :(得分:1)

正如@DanielRoseman所提到的,您不需要在ajax代码中使用contentType