David Brierton David Brierton - 6 months ago 16
jQuery Question

Ajax call SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

I am trying to make an ajax call using jquery and setting session variables using Coldfusion 10. This is the error I am receiving:

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data


enter image description here

Am I overlooking something?


$('#ToolTables_processing_1').on('click', function () {
$.ajax({
// the location of the CFC to run
url: "redirects/selectedrows.cfm",
// send a GET HTTP operation
type: "post",
// tell jQuery we're getting JSON back
dataType: "json",
// send the data to the CFC
data: $('#form').serialize(),
// this gets the data returned on success
success: function (data) {
console.log(data);
window.location = 'forms/exitinterviewpdf.cfm';
},
// this runs if an error
error: function (xhr, textStatus, errorThrown) {
// show error
console.log(errorThrown);
}
});
});


If I take out the
datatype: json
I just get an empty string.
enter image description here

selectedrows.cfm

<cfset session.exitinterview.selected.selectedRowName = form.selectedRowName >
<cfset session.exitinterview.selected.selectedRowtodayDate = form.selectedRowtodayDate >
<cfset session.exitinterview.selected.selectedRowtitle = form.selectedRowtitle >
<cfset session.exitinterview.selected.selectedRowdepartment = form.selectedRowdepartment >
<cfset session.exitinterview.selected.selectedRowhireDate = form.selectedRowhireDate >
<cfset session.exitinterview.selected.selectedRowterminationDate = form.selectedRowterminationDate >
<cfset session.exitinterview.selected.selectedRowreasonLeaving = form.selectedRowreasonLeaving >
<cfset session.exitinterview.selected.selectedRowfeelPay = form.selectedRowfeelPay >
<cfset session.exitinterview.selected.selectedRowprogressHere = form.selectedRowprogressHere >
<cfset session.exitinterview.selected.selectedRowanotherJob = form.selectedRowanotherJob >
<cfset session.exitinterview.selected.selectedRowcomparewith = form.selectedRowcomparewith >
<cfset session.exitinterview.selected.selectedRowhigherSalary = form.selectedRowhigherSalary >
<cfset session.exitinterview.selected.selectedRowpreventLeaving = form.selectedRowpreventLeaving >
<cfset session.exitinterview.selected.selectedRowclearlyExplained = form.selectedRowclearlyExplained >
<cfset session.exitinterview.selected.selectedRowbyWhom = form.selectedRowbyWhom >
<cfset session.exitinterview.selected.selectedRowadequateTraining = form.selectedRowadequateTraining >
<cfset session.exitinterview.selected.selectedRowworkPerform = form.selectedRowworkPerform >
<cfset session.exitinterview.selected.selectedRowworkingCondition = form.selectedRowworkingCondition >
<cfset session.exitinterview.selected.selectedRowsalary = form.selectedRowsalary >
<cfset session.exitinterview.selected.selectedRowretirement = form.selectedRowretirement >
<cfset session.exitinterview.selected.selectedRowhealthPlan = form.selectedRowhealthPlan >
<cfset session.exitinterview.selected.selectedRowtuition = form.selectedRowtuition >
<cfset session.exitinterview.selected.selectedRowunileave = form.selectedRowunileave >
<cfset session.exitinterview.selected.selectedRowcoworkers = form.selectedRowcoworkers >
<cfset session.exitinterview.selected.selectedRowsupervisionReceived = form.selectedRowsupervisionReceived >
<cfset session.exitinterview.selected.selectedRowneededHelp = form.selectedRowneededHelp >
<cfset session.exitinterview.selected.selectedRowmanagerRespond = form.selectedRowmanagerRespond >
<cfset session.exitinterview.selected.selectedRowsuggestionsBetterPlace = form.selectedRowsuggestionsBetterPlace >
<cfset session.exitinterview.selected.selectedRowrealReasonForLeaving = form.selectedRowrealReasonForLeaving >
<cfset session.exitinterview.selected.selectedRowcomments = form.selectedRowcomments >

<cfoutput>#SerializeJSON(session.exitinterview.selected)#</cfoutput>


Errors where its breaking
enter image description here

enter image description here

For VLAD
enter image description here

Answer

What I usually do when debugging large data sets is narrow down the problem. Please try the sample codes

 <!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>

<body>

<form id="form" method="post" onsubmit="javascript:return(false)">

    <input type="text" name="SELECTEDROWNAME" id="SELECTEDROWNAME" value="Vlad" >   
    <input type="submit" id="ToolTables_processing_1" value="save" >    

</form>


<script type="text/javascript">

$(function(){

    $('#ToolTables_processing_1').on('click', function () {

        $.ajax({
            // the location of the CFC to run
            url: "redirects/selectedrows.cfm",
            // send a GET HTTP operation
            type: "post",
            // tell jQuery we're getting JSON back
            dataType: "json",
            // send the data to the CFC
             data: $('#form').serialize(),
            // this gets the data returned on success
            success: function (data) {
                console.log(data);
                //window.location = 'forms/exitinterviewpdf.cfm';
            }, 
            // this runs if an error
            error: function (xhr, textStatus, errorThrown) {
                // show error
                console.log(errorThrown);
            }
        });
    });
});//doc ready

</script>
</body>

</html>

selectedrows.cfm

    <cfset session.exitinterview.selected.selectedRowName = form.selectedRowName >
<!---<cfset session.exitinterview.selected.selectedRowtodayDate = form.selectedRowtodayDate >
<cfset session.exitinterview.selected.selectedRowtitle = form.selectedRowtitle >
<cfset session.exitinterview.selected.selectedRowdepartment = form.selectedRowdepartment >
<cfset session.exitinterview.selected.selectedRowhireDate = form.selectedRowhireDate >
<cfset session.exitinterview.selected.selectedRowterminationDate = form.selectedRowterminationDate >
<cfset session.exitinterview.selected.selectedRowreasonLeaving = form.selectedRowreasonLeaving >
<cfset session.exitinterview.selected.selectedRowfeelPay = form.selectedRowfeelPay >
<cfset session.exitinterview.selected.selectedRowprogressHere = form.selectedRowprogressHere >
<cfset session.exitinterview.selected.selectedRowanotherJob = form.selectedRowanotherJob >
<cfset session.exitinterview.selected.selectedRowcomparewith = form.selectedRowcomparewith >
<cfset session.exitinterview.selected.selectedRowhigherSalary = form.selectedRowhigherSalary >
<cfset session.exitinterview.selected.selectedRowpreventLeaving = form.selectedRowpreventLeaving >
<cfset session.exitinterview.selected.selectedRowclearlyExplained = form.selectedRowclearlyExplained >
<cfset session.exitinterview.selected.selectedRowbyWhom = form.selectedRowbyWhom >
<cfset session.exitinterview.selected.selectedRowadequateTraining = form.selectedRowadequateTraining >
<cfset session.exitinterview.selected.selectedRowworkPerform = form.selectedRowworkPerform >
<cfset session.exitinterview.selected.selectedRowworkingCondition = form.selectedRowworkingCondition >
<cfset session.exitinterview.selected.selectedRowsalary = form.selectedRowsalary >
<cfset session.exitinterview.selected.selectedRowretirement = form.selectedRowretirement >
<cfset session.exitinterview.selected.selectedRowhealthPlan = form.selectedRowhealthPlan >
<cfset session.exitinterview.selected.selectedRowtuition = form.selectedRowtuition >
<cfset session.exitinterview.selected.selectedRowunileave = form.selectedRowunileave >
<cfset session.exitinterview.selected.selectedRowcoworkers = form.selectedRowcoworkers >
<cfset session.exitinterview.selected.selectedRowsupervisionReceived = form.selectedRowsupervisionReceived >
<cfset session.exitinterview.selected.selectedRowneededHelp = form.selectedRowneededHelp >
<cfset session.exitinterview.selected.selectedRowmanagerRespond = form.selectedRowmanagerRespond >
<cfset session.exitinterview.selected.selectedRowsuggestionsBetterPlace = form.selectedRowsuggestionsBetterPlace >
<cfset session.exitinterview.selected.selectedRowrealReasonForLeaving = form.selectedRowrealReasonForLeaving >
<cfset session.exitinterview.selected.selectedRowcomments = form.selectedRowcomments >--->
<cfsetting showdebugoutput="false">
<cfcontent reset="true" >
<cfoutput>#SerializeJSON(session.exitinterview.selected)#</cfoutput>

As others suggested it can be some malformed JSON data and as you figured it out some code in the Application.cfc is the problem.