语法错误:输入parseJSON的意外结束

时间:2014-08-07 03:40:52

标签: javascript php jquery html ajax

我大约在第5小时,并认为是时候寻求帮助了。我正在尝试使用AJAX + php将表单上的图像和一些文本数据上传到数据库。整个系统是:

带有表单social.php的输入页面 一个php处理页面,postMsg.php 和一个javascript函数postMsg(),它将表单发布到php处理页面,并且应该将结果返回到social.php上的div

问题是javascript中的$ .parseJSON(data)命令导致“意外的输入结束”错误:

Failed:  
SyntaxError {stack: (...), message: "Unexpected end of input"}
(index):156
Uncaught SyntaxError: Unexpected end of input jquery.js:4235
b.extend.parseJSON jquery.js:4235
(anonymous function) (index):158
c jquery.js:4611
p.fireWith jquery.js:4687
k jquery.js:10335
r

我认为我的javascript存在问题,但我对其进行了代码检查,并且没关系:

     function postMsg() {
        console.log("submit event");
        var fd = new FormData(document.getElementById("commentSubmit"));
        fd.append("label", "WEBUPLOAD");
        document.getElementById('progressBar').style.display = 'block';

        $.ajax({
          url: "postMsg.php",
          type: "POST",
          xhr: function() {  // Custom XMLHttpRequest
            var myXhr = $.ajaxSettings.xhr();
            if(myXhr.upload){ // Check if upload property exists
                myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
            }
            return myXhr;
          },
          data: fd,
          enctype: 'multipart/form-data',
          processData: false,  // tell jQuery not to process the data
          contentType: false   // tell jQuery not to set contentType
        }).done(function( data ) {
            console.log("PHP Output:");
            console.log( data );

            try {responseData = $.parseJSON(data)}
            catch (e) {console.log('Failed: ', e, data);}

            var items = $.parseJSON(data);
            document.getElementById('progressBar').style.display = 'none';
        });
        return false;
    }

然后我认为我的php存在问题,但是用一个简单的命令替换了它,它仍然导致了同样的错误:

$json_array = array('selfie'=>'hello');

然后我认为我的输入表单可能有问题,所以我重写了,但它仍然返回相同的错误:

echo '<div data-role="fieldcontain" style="margin-top: -30px;margin-bottom: -30px;border-bottom: 0px;">
                <form method="post" name="commentSubmit" id="commentSubmit">
                  <div style="width=100%; font-size:.9em;" data-role="fieldcontain">
                  <label class="ui-input-text" for="msg_txt">Chip in:</label>';

//          $selfie = get_selfie($uid);
        echo '<div style="margin-top: 10px; margin-bottom: 10px; display: block; font-size:.9em">';

echo '<input name="file" type="file">';
        echo '<textarea style="width:100% text-align:left; font-weight:normal;" class="ui-btn ui-btn-inline ui-btn-icon-notext ui-btn-up-c" data-iconshadow="true" data-shadow="true" data-corners="false" cols="23" rows="1" name="msg_txt" id="msg_txt"></textarea>';
        echo '<a style="border-radius:8px" class="ui-btn ui-btn-inline ui-btn-icon-notext ui-corner-right ui-controlgroup-last ui-btn-up-c" title="My button" data-wrapperels="span" data-iconshadow="true" onclick="postMsg();" data-shadow="true" data-corners="true" data-role="button" data-icon="search" data-iconpos="notext" data-theme="c" data-inline="true"><span class="ui-btn-inner ui-corner-right ui-controlgroup-last"><span class="ui-btn-text">My button</span><span class="ui-icon ui-icon-search ui-icon-shadow">&nbsp;</span></span></a>';
        echo '<div id="photoUploaded" style="display: none;
position: relative;
text-align: center;
background-color: white;
opacity: .5;
color: black;
float: right; 
vertical-align: middle;
font-family: sans-serif;
border-radius: 10px;
padding: 10px;">photo loaded</div>';

                  echo '<input name="refresh" value="1" id="refresh" type="hidden">
                    <input name="uname" value="'.get_name($uid).'" id="uname" type="hidden">
                    <input name="uid" value="'.$uid.'" id="uname" type="hidden">

                </form>

有什么想法吗?

1 个答案:

答案 0 :(得分:12)

这让我困扰了一段时间,因为同一个问题的大部分答案都被抓到了兔子洞。另外,答案在ajax对象的jquery文档中隐藏 DEEP 。没有进一步的麻烦:

jQuery.ajax:dataType

  

&#34; json&#34;:将响应评估为JSON并返回JavaScript对象。 JSON数据以严格的方式解析;任何格式错误的JSON都会被拒绝,并抛出一个解析错误。从jQuery 1.9开始,空响应也被拒绝;服务器应该返回null或{}的响应。 (有关正确的JSON格式的更多信息,请参阅json.org。)

因此,您必须始终至少返回一个空的JSON对象,或者从任何请求返回null,以便jquery将其视为有效。

相关问题