mob*_*bro 9 html javascript php ajax jquery
我大约在第5个小时,并认为是时候寻求帮助了.我正在尝试使用AJAX + php将表单上的图像和一些文本数据上传到数据库.整个系统是:
一个带有表单的输入页面,social.php
一个php处理页面postMsg.php
和一个javascript函数postMsg(),它将表单发布到php处理页面,并且应该将结果返回给divsocial.php
问题是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
Run Code Online (Sandbox Code Playgroud)
我以为我的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;
}
Run Code Online (Sandbox Code Playgroud)
然后我认为我的php存在问题,但是用一个简单的命令替换了它,它仍然导致了同样的错误:
$json_array = array('selfie'=>'hello');
Run Code Online (Sandbox Code Playgroud)
然后我想我的输入表单可能有问题,所以我重写了,但它仍然返回相同的错误:
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>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
Bre*_*dly 12
这让我困扰了一段时间,因为大多数同样问题的答案都被抓到了兔子洞.另外,答案隐藏在ajax对象的文档中DEEPjquery.无需再费周折:
"json":将响应计算为JSON并返回JavaScript对象.JSON数据以严格的方式解析; 任何格式错误的JSON都会被拒绝,并抛出一个解析错误.从jQuery 1.9开始,空响应也被拒绝; 服务器应该返回null或{}的响应.(有关正确的JSON格式的更多信息,请参阅json.org.)
因此,您必须始终至少返回一个空的JSON对象,或者从任何请求返回null,以便将jquery其接受为有效.
| 归档时间: |
|
| 查看次数: |
16603 次 |
| 最近记录: |