0

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 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"]}
6
  • 1
    Are you getting the error in JQuery or in python script? Commented May 23, 2015 at 0:09
  • Not sure I get it, do you have issues stringifying an object to JSON with javascript, if so what does the ajax call have to do with it? If you remove everything but JSON.stringify(data_cp) do you still get the same error ? Commented May 23, 2015 at 0:10
  • @codesnooker - It is coming from alert(xhr.status); alert(thrownError) ,if I comment this out the error goes way but the call doesnt goto success:function either Commented May 23, 2015 at 0:13
  • @adeneo - the error is coming from the ajax call as stated in above comment.. Commented May 23, 2015 at 0:15
  • 1
    Then you're not returning valid JSON from the serverside Commented May 23, 2015 at 0:16

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I updated the server side python script..the thing is I have a variable ` result = {'success':'true','message':'The Command Completed Successfully'} ` in JSON format and in the end I try to ` print json.dumps(result)` so that script outputs in JSON format ,for some reason the print is not working..this has worked for me in previous cases,not sure why its failing now?
Can you add a check if you are reaching to the statement: print json.dumps(result); If yes then what is the output if you don't use json.dumps as simply print the result?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.