【发布时间】:2015-10-10 20:12:12
【问题描述】:
我正在尝试通过 ajax POST 请求将字符串从 Python 传递到 Javascript,但我遇到了严重的困难。
我尝试过使用和不使用 JSON。
这是代码
JAVASCRIPT
$.ajax({
url: url, #url of the python server and file
type: "POST",
data: {'data1': "hey"},
success: function (response) {
console.log(" response ----> "+JSON.parse(response));
console.log(" response no JSON ---> " +response);
},
error: function (xhr, errmsg, err) {
console.log("errmsg");
}
});
Python
import json
print "Access-Control-Allow-Origin: *";
if form.getvalue("data1") == "hey":
out = {'key': 'value', 'key2': 4}
print json.dumps(out)
结果是一个空的 JSON。当我在 javascript 中执行 JSON.parse 之类的操作时,我得到一个意外的输入错误结束,当我尝试获取响应数据的长度时,我得到的大小为 0。 我想客户端服务器通信应该有一些问题(我使用 CGIHTTPServer)或者 python 或 javascript 期望的数据类型可能有问题。
我也试过不使用 JSON,比如 蟒蛇
print "heyyyyy"
Javascript
alert(response) //case of success
但我也得到了一个空字符串。
你能给我一些处理这个问题的建议吗? 非常感谢!
【问题讨论】:
-
你不应该使用打印。
标签: javascript jquery ajax json python-2.7