gpt4 book ai didi

javascript - 语法错误 : unexpected end of input parseJSON

转载 作者:太空狗 更新时间:2023-10-29 13:25:05 26 4
gpt4 key购买 nike

我已经进行了大约 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>

有什么想法吗?

最佳答案

这困扰了我一段时间,因为同一问题的大部分答案都陷入了困境。此外,答案隐藏在 ajax 对象的 jquery 文档中。事不宜迟:

jQuery.ajax:dataType

"json": Evaluates the response as JSON and returns a JavaScript object. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

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

关于javascript - 语法错误 : unexpected end of input parseJSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25173727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com