【发布时间】:2015-08-05 02:46:51
【问题描述】:
我有以下 ajax 调用,我在其中以 JSON 格式传递数据,当执行此代码时,我收到如下所示的错误,我在下面显示了 Console.log(data_cp),并在 http://jsonlint.com/ 中验证了它这是经过验证的输入?我在这里遗漏了什么?如何解决此错误?我查看了其他帖子,例如 json parsing error syntax error unexpected end of input,但无法弄清楚...
$.ajax({
dataType: "json",
type: "POST",
contentType: "application/json",//note the contentType defintion
url: "scripts/cherrypick.py",
data: JSON.stringify(data_cp),
//data: data_cp,
error : function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
},
success: function(message){
console.log("cherypick sucess");
}
服务器端 python 脚本:-
#!/usr/bin/python
import os
import sys
import json
print "Content-type: application/json\n\n"
...............
...............
def main():
result = {'success':'true','message':'The Command Completed Successfully'}
cherrypicklist = []
cherrypickfaillist = []
myjson = json.load(sys.stdin)
gerritlist = myjson['gerrits']
resource = r'buildserver'
buildlocation = r'cd /local/mnt/workspace/user/buildlocation ; '
for gerrit in gerritlist:
cmd = buildlocation
project,ref = fetchgerritproject(gerrit, connection=None)
proj_path = getprojectpath(project)
cmd += 'cd ' + proj_path + ' ;'
new_cmd = ' gknife am-or-cp ' + gerrit
pick_cmd = cmd + new_cmd
errorlist =''
errorlist = cherrypick(resource,pick_cmd)
if len(errorlist) <= 2:
cherrypicklist.append(gerrit)
else:
chk_cmd = cmd + ' git checkout -f'
connection = ssh_connect(resource)
errorlist = execute_command(connection,chk_cmd)
cherrypickfaillist.append(gerrit)
for gerrit in cherrypicklist:
cmd = buildlocation
project,ref = fetchgerritproject(gerrit, connection=None)
proj_path = getprojectpath(project)
cmd += ' cd ' + proj_path + ' ;'
errorlist = resetgerrit(resource,cmd)
errorlist = execute_command(connection,chk_cmd)
print json.dumps(result)
#return
if __name__ == "__main__":
main()
错误:-
SyntaxError: Unexpected end of input
Console.log(data_cp) 输出:-
{"gerrits":["1258565","1279604"]}
【问题讨论】:
-
您是否在 JQuery 或 python 脚本中收到错误?
-
不知道我明白了,你有没有使用 javascript 将对象字符串化为 JSON 的问题,如果是,ajax 调用与它有什么关系?如果您删除了除
JSON.stringify(data_cp)之外的所有内容,您还会收到相同的错误吗? -
@codesnooker - 它来自
alert(xhr.status); alert(thrownError),如果我将其注释掉,错误就会发生,但调用也不会成功:函数 -
@adeneo - 错误来自上述评论中所述的 ajax 调用..
-
那你不是从服务器端返回有效的JSON
标签: javascript python json