这是我的Jquery代码
function kirimgambar(chatboxtitle) {
var file = jQuery( ".pilih" ).val();
if(file==""){
jQuery("#chatboxtextarea").focus();
}else {
jQuery("#loading")
.ajaxStart(function(){
jQuery(this).show();
})
.ajaxComplete(function(){
jQuery(this).hide();
});
jQuery.ajaxFileUpload
(
{
url:'http://localhost:8080/diobatin/application/views/chat/chat.php?action=kirimfile',
secureuri:false,
fileElementId:'fileToUpload',
dataType: 'json',
data:{name:'logan', id:'id', chatboxtitle:chatboxtitle},
success: function (data, status)
{
if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
alert(data.msg);
}
}
},
error: function (data, status, e)
{
alert(e); //This ouput is "syntax error unexpected token <"
}
}
)
chatHeartbeatTime = minChatHeartbeat;
chatHeartbeatCount = 1;
return false;
}
}
在chat.php中
function kirimfile() {
$error = "";
$msg = "";
$fileElementName = 'fileToUpload';
if(!empty($_FILES[$fileElementName]['error']))
{
$error = 'No file was uploaded..';
}elseif(empty($_FILES['fileToUpload']['tmp_name']) || $_FILES['fileToUpload']['tmp_name'] == 'none')
{
$error = 'No file was uploaded..';
}else
{
$upload_path = "./asset/";
if (!is_dir($upload_path)) {
mkdir($upload_path);
}
$file = $_FILES['fileToUpload']['name'];
$tmp = $_FILES['fileToUpload']['tmp_name'];
// jika $file ada dan tidak kosong
if ((isset($file)) && ($file != "")) {
// handle apabila sudah ada file sama yang terupload, maka akan dibuat copynya
$uploadfile = (file_exists($upload_path.$file)) ? $upload_path." copy of ".$file : $upload_path.$file;
move_uploaded_file($tmp, $uploadfile);
if (chmod($uploadfile, 0775)) {
$from = $_SESSION['username'];
$to = $_POST['chatboxtitle'];
$message = $_SESSION['username'];
$messagesan = sanitize($message);
if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
$_SESSION['chatHistory'][$_POST['to']] = '';
}
$_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
{
"s": "1",
"f": "{$to}",
"m": "{$messagesan}"
},
EOD;
unset($_SESSION['tsChatBoxes'][$_POST['to']]);
}
}
}
echo '<meta http-equiv="Content-type" content="text/html; charset=UTF-8">';
echo "{\n";
echo "error: '" . $error . "',\n";
echo "msg: '" . $msg . "'\n";
echo "}";
}
alern上的此输出返回语法错误意外令牌&lt; chrome 但是在 mozilla firefox 错误报告是 未声明框架文档的字符编码。如果在没有文档框架的情况下查看,该文档可能会显示不同。
答案 0 :(得分:0)
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
和\n
不是JSON。
替换
echo '<meta http-equiv="Content-type" content="text/html; charset=UTF-8">';
echo "{\n";
echo "error: '" . $error . "',\n";
echo "msg: '" . $msg . "'\n";
echo "}";
使用
$arr = array(
'error' => $error,
'msg' => $msg
);
echo json_encode($arr);