gpt4 book ai didi

javascript - 未捕获的语法错误 : Unexpected token < (anonymous function) on console

转载 作者:行者123 更新时间:2023-12-03 11:19:29 24 4
gpt4 key购买 nike

我正在创建一个上传文件应用程序。我使用 AJAX 和 PHP 编写了它。

它在本地主机上工作正常,但是当我将它上传到我的网络服务器时。它返回错误:

Uncaught SyntaxError: Unexpected token <

它指向该线

uploaded = JSON.parse(this.response);

此行位于我的 upload.js 脚本文件中

upload.js

var app = app || {};
(function (obj) {
"use stricts;"
var ajax, getFormData, setProgress;
ajax = function(data){
var xmlhttp = new XMLHttpRequest(), uploaded;
xmlhttp.addEventListener('readystatechange', function(){
if (this.readyState === 4) {
if (this.status === 200) {
uploaded = JSON.parse(this.response);
if (typeof obj.options.finished === 'function') {
obj.options.finished(uploaded);
}
}else{
if (typeof obj.options.error === 'function') {
obj.options.error();
}
}
}
});
xmlhttp.upload.addEventListener('progress',function(){
var percent;

if (event.lengthComputable === true) {
percent = Math.round((event.loaded / event.total) * 100);
setProgress(percent);
}
});

xmlhttp.open('post', obj.options.processor);
xmlhttp.send(data);

};

getFormData = function(source){
var data = new FormData(), i;

for(i=0; i<source.length; i = i+1){
data.append('file[]',source[i]);
}
data.append('ajax', true);
return data;
};

setProgress = function (value){
if (obj.options.progressBar !== undefined) {
obj.options.progressBar.style.width = value ? value + '%': 0;
}

if (obj.options.progressText !== undefined) {
obj.options.progressText.innerText = value ? value + '%' : 0;
}
};

obj.uploader = function(options){
obj.options = options;

if (obj.options.files !== undefined) {
ajax(getFormData(obj.options.files.files));
}
}
}(app));

以下是其他代码供引用

上传.php

 <?php
header('Content-Type: application/JSON');
$uploaded = [];
$allowed = ['jpg'];
$succeeded = [];
$failed = [];
if (!empty($_FILES['file'])) {

foreach ($_FILES['file']['name'] as $key => $name) {

if($_FILES['file']['error'][$key] === 0){
$temp = $_FILES['file']['tmp_name'][$key];
$ext = explode('.', $name);
$ext = strtolower(end($ext));

$file = md5_file($temp) . time() .'.'.$ext;

if (in_array($ext,$allowed) === true && move_uploaded_file($temp, "uploads/{$file}") === true) {
$succeeded [] = array('name' => $name, 'file' => $file);

# code...
}else{
$failed[] = array('name' => $name );
}

}else{

echo "Error";
}
}
}

if (!empty($_POST['ajax'])) {
echo json_encode(array(
'succeeded' => $succeeded,
'failed' =>$failed
));
}
?>

这是我的 html 表单

index.php

<form action="upload.php" method="post" enctype="multipart/form-data" id="upload" class="upload">
<fieldset>
<legend>Upload Files</legend>
<input type="file" id="file" name="file[]" required multiple>
<input type="button" id="submit" value="Upload">
</fieldset>
<div class="bar">
<span class="barfill" id="pb"><span class="barfilltext" id="pt">40%</span></span>
</div>
<div id="uploads" class="uploads">

</div>
<script type="text/javascript" src="upload.js"></script>

<script type="text/javascript">
document.getElementById('submit').addEventListener('click', function(e){
e.preventDefault();
var f = document.getElementById('file'),
pb = document.getElementById('pb'),
pt = document.getElementById('pt');

app.uploader({
files:f,
progressBar:pb,
progressText:pt,
processor: 'upload.php',

finished: function(data){
var uploads = document.getElementById('uploads'),
succeeded = document.createElement('div'),
failed = document.createElement('div'), anchor, span, x;

if (data.failed.length) {
failed.innerHTML = '<p>The following files failed to upload</p>'
}
uploads.innerText = '' ;
anchor = document.createElement('p');
anchor.innerText = "Upload Completed!";
anchor.target = '_blank';
succeeded.appendChild(anchor);
for(x=0;x<data.failed.length; x=x+1){
span = document.createElement('span');
span.innerText = data.failed[x].name;
failed.appendChild(span);
}
uploads.appendChild(succeeded);
uploads.appendChild(failed);
},
error: function (){
console.log("Error");
}
});
});
</script>
</form>

此代码在本地主机上运行。它正在将文件上传到我的本地服务器并显示加载进度条。

但是当我将其部署到我的网络服务器时,它显示进度条加载缓慢,直到达到 100%。但是当我查看服务器中的上传目录时,没有任何内容上传。

最佳答案

upload.php 中的代码末尾、php 结尾 (?>) 之前缺少 }:

'failed' =>$failed
));
}
}
?>

关于javascript - 未捕获的语法错误 : Unexpected token < (anonymous function) on console,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27187314/

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