JSON SyntaxError:输入的意外结束

JSON SyntaxError: Unexpected end of input

本文关键字:意外 结束 输入 SyntaxError JSON      更新时间:2023-09-26

我有以下ajax调用,我在JSON格式中传递数据,当执行此代码时,我得到下面所示的错误,我显示了下面的Console.log(data_cp),我在http://jsonlint.com/中验证了它,它是一个经过验证的输入?我错过了什么?如何解决这个错误?我看了其他的帖子,如json解析错误语法错误意外输入结束,但无法找出…

        $.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文档中错误方法的定义,如果调用不成功,您将从服务器端获得错误。

所以这意味着你从服务器得到错误。检查服务器代码

从JQuery定义错误方法

error Type: Function(jqXHR jqXHR, String textStatus, String . error请求失败时调用的函数。的函数接收三个参数:jqXHR(在jQuery 1.4.x中,XMLHttpRequest)对象,它是一个描述错误类型的字符串发生和可选异常对象(如果发生)。可能的第二个参数的值(除了null)是"timeout", "error","abort"answers"parsererror"。当HTTP错误发生时,errorThrown接收HTTP状态的文本部分,例如"未找到"。或"内部服务器错误"。从jQuery 1.5开始,错误设置可以接受一个函数数组。每个函数将依次调用。注意:这个处理程序不会被跨域脚本调用跨域JSONP请求。这是一个Ajax事件