我正在尝试借助 AJAX 将图像文件异步上传到服务器。该文件通过 FIREFOX 成功上传,但无法通过 Google Chrome 上传。
它显示以下错误消息:未捕获的语法错误,输入意外结束
谷歌搜索导致我可能会丢失一两个括号,但我无法在我的代码中找到任何此类错误。帮助?
这是代码:
<input type="file" name="profile_image" onchange="uploadedImage(this)"/>
function uploadedImage(image)
{
var file = image.files[0];
var name = file.name;
var size = file.size;
var type = file.type;
var msg='';
if(size>500000)
msg+="File should not be greater than 500kb";
if(type!="image/jpeg")
msg+="Only JPEG images are currently supported";
if(msg!='')
{
alert(msg);
return;
}
var formData = new FormData(document.getElementById('edit_settings'));
$.ajax(
{
url: 'upload_demo_image.php', //Server script to process data
type: 'POST',
xhr: function()
{ // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
return myXhr;
},
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false,
success: function(filename)
{
var response = jQuery.parseJSON(filename);
var newImage=response['filename'][0];
$("#edit_profile_image_container").html("<img id='edit_profile_image' src='"+newImage+"' alt='Profile Image'/>");
crop();
}
}
);
}
这是从服务器返回的 JSON 数据:
{"filename":["uploadDemo\/8_demo.jpg"]}
PHP代码:
<?php session_start();
require_once 'myfunctions.php';
if(isset($_FILES["profile_image"]["name"]))
{
$query="SELECT * FROM students WHERE email='$_SESSION[user]'";
$result=queryMysql($query);
$row=mysql_fetch_array($result);
$_FILES["profile_image"]["name"]=$row['studentid']."_demo.jpg";
$saveto="uploadDemo/$row[studentid]_demo.jpg";
$tempname=$_FILES["profile_image"]["tmp_name"];
$type=$_FILES["profile_image"]["type"];
$image_check=uploadimage($tempname,$saveto,$type,600);
$filename["filename"][]=$saveto;
header('Content-type :text/JSON');
echo json_encode($filename);
}
?>