JSON SyntaxError:inpu意外结束

2024-04-28 12:46:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的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

控制台.log(data_cp)输出:

{"gerrits":["1258565","1279604"]}

Tags: pathprojectcmdlogjsondata错误error
1条回答
网友
1楼 · 发布于 2024-04-28 12:46:46

根据Jquery文档中错误方法的定义,如果调用不成功,则从服务器端获取错误。

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

从JQuery定义错误方法

error Type: Function( jqXHR jqXHR, String textStatus, String errorThrown ) A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event.

相关问题 更多 >