JSON SyntaxError: Unexpected end of input
Question: I have the following ajax call where I am passing the data in JSON format and
when this code gets executed I get the error shown below,I showed the
Console.log(data_cp) below and I validated it in <http://jsonlint.com/> and it
is a validated input?what am I missing here?how to fix this error?I looked at
other posts like [json parsing error syntax error unexpected end of
input](http://stackoverflow.com/questions/20827372/json-parsing-error-syntax-
error-unexpected-end-of-input) but couldnt figure out...

    
    
            $.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");        
                }
    

Serverside python script:-

    
    
    #!/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()
    

Error:-

    
    
    SyntaxError: Unexpected end of input
    

Console.log(data_cp) output:-

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


Answer: As per definition of error method from Jquery docs, you get error from server
side or if call is not successful.

So it means you are getting error from server. Check the server code.

**Definition of Error method from 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.

