Ajax调用SyntaxError:JSON.parse:JSON数据的第1行第1列的意外数据结尾

时间:2016-05-25 00:08:02

标签: jquery json ajax coldfusion

我正在尝试使用jquery进行ajax调用并使用Coldfusion 10设置会话变量。这是我收到的错误:
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

enter image description here

我忽略了什么吗?

$('#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);
        }
    });
});

如果我取出datatype: json,我就会得到一个空字符串。 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>

破坏的错误 enter image description here

enter image description here

对于VLAD enter image description here

1 个答案:

答案 0 :(得分:1)

调试大型数据集时我通常会做的是缩小问题范围。请尝试示例代码

 <!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>

正如其他人所说的那样,它可能是一些格式错误的JSON数据,并且当你发现它时,Application.cfc中的一些代码就是问题。