[英]syntax error: unexpected end of input parseJSON
我大約在第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"> </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>
有任何想法嗎?
這讓我困擾了一段時間,因為大多數同樣問題的答案都被抓到了兔子洞。 另外,答案是隱藏在ajax對象的jquery
文檔中的DEEP 。 無需再費周折:
“json”:將響應計算為JSON並返回JavaScript對象。 JSON數據以嚴格的方式解析; 任何格式錯誤的JSON都會被拒絕,並拋出一個解析錯誤。 從jQuery 1.9開始,空響應也被拒絕; 服務器應該返回null或{}的響應。 (有關正確的JSON格式的更多信息,請參閱json.org。)
因此,您必須始終至少返回一個空的JSON對象,或者從任何請求返回null,以便jquery
將其接受為有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.