所以我正在使用 AJAX 使用我正在构建的这个图像管理器工具上传图像......突然它不起作用......我没有更改任何代码或任何东西。
.php 运行并上传图像,但我想在 json 编码并发回后触发的事件并没有发生。:/
控制台日志Uncaught SyntaxError: Unexpected token <
同样的事情发生在另一个 AJAX 请求上,但在第三个请求上很好......
过去,当我收到此错误时,这是一个 PHP 语法错误,但我有一段时间没有更新 .php 了.. soooo 我难住了。
这是我的Javascript:
$("#chosenfile").change(function(){
if($(this).attr('name')) {
var data;
data = new FormData();
data.append('uploadedfile', $( '#chosenfile' )[0].files[0]);
$('#loader').css('display','block');
$.ajax({
url: '/includes/uploadimage.php',
data: data,
processData: false,
contentType: false,
type: 'POST',
success: function ( data ) {
//console.log(data); //returns a string of the data.
var image = JSON.parse(data); //parses the string into an object.
console.log(image); //logs the object.
if (image.error) {
alert(image.error);
$('#remove').click();
$('#loader').css('display','none');
} else {
if (image.link) { //If the image is uploaded and returned a link.
$('#remove').click();
$('#loader').css('display','none');
$('#scrollwindow').append("<div class='you'><img imgid='" + image.id + "' src='" + image.link + "' alt=''><span class='delete'>x</span></div>").fadeIn(200);
addToSlider(1);
};
}
}
});
}
});
这是我的PHP:
<?php
include '../includes/global.php'; // General Vars and Functions I use sitewide.
/* ------------------------- */
/* -- Lets Get that file! -- */
/* ------------------------- */
$file = $_FILES["uploadedfile"];
$Return = array();
$status = "failure";
/* ------------------------- */
/* - Okay, lets upload it! - */
/* ------------------------- */
$Return["type"] = $file["type"];
$target_path = "../uploads/"; // Define a folder to upload to
$allowedExts = array("jpeg", "jpg", "JPG", "JPEG", "image/jpg"); // Array of allowed extensions.
$extension = end(explode("/", $file["type"])); // Find extension.
$maxsize = 2097152; // Get Max file size from hidden field in form.
if ($file) {
if (in_array($extension, $allowedExts)) // If the extension is allowed.
{
if(($file["size"] < $maxsize)) { // If size is less then max file size.
$uploadResponse = "The file is a good size - ";
$target_path = $target_path . basename($file['name']); //Add file name string to upload folder destination string.
$imageLink = "http://scoutsamerica.com/uploads/" . basename( $file['name']); // This is the full link
if (file_exists($target_path)) { // If that file already exists, add a random integer to the end of it after a "_".
$uploadResponse .= "This file exists: " . $target_path;
$randomNum = rand(1, 9999999);
$crudparts = explode(".", $file["name"]); //split filename and extension again.
$exceptExt = $crudparts[0]; //Filename
$extension = $crudparts[1]; //Extension
$target_path = "../uploads/" . $exceptExt . "_" . $randomNum . "." . $extension; //rename it with the random number.
$imageLink = "http://scoutsamerica.com/uploads/" . $exceptExt . "_" . $randomNum . "." . $extension;
$uploadResponse .= " Path changed to: " . $target_path;
}
if(move_uploaded_file($file['tmp_name'], $target_path)) {
$uploadResponse .= "The file ". basename( $file['name']) . " has been uploaded to " . $target_path . "</br></br>";
} else {
$uploadResponse .= "There was an error uploading the file, please try again! </br>";
$uploadResponse .= "File size: " . ($file["size"] / 1024) . "kb</br>";
$uploadResponse .= "Max size: " . ($maxsize / 1024) . "kb";
}
$status = "success";
$Return["link"] = $imageLink;
/* ---------------------------------- */
/* - Time to upload that image, yo. - */
/* ---------------------------------- */
$imageSql = "INSERT INTO images (id, link, model_id, addedon) VALUES (".
"null, " .
PrepSQL($imageLink) . ", " .
PrepSQL($myId) . ", " .
PrepSQL($date) . ")";
mysql_query($imageSql) or die("Images: " . mysql_error());
} else {
$uploadResponse = "The file must less then " . ($maxsize / 1024) . "kb.";
$status = "failure";
$Return["error"] = $uploadResponse;
}
} else {
$uploadResponse = "The file must be a .jpg file.";
$status = "failure";
$Return["error"] = $uploadResponse;
}
} else {
$uploadResponse = "No Image.";
$status = "failure";
$Return["error"] = $uploadResponse;
}
/* ------------------------- */
/* - Lets Send it back :) - */
/* ------------------------- */
$Return["status"] = $status;
$Return["id"] = mysql_insert_id();
$Return["traveler"] = $file;
str_replace('\\/', '/', json_encode($Return));
echo json_encode($Return);
?>
原始响应:
<script>
function hairEyesFind(haireyes){
if (haireyes == "blonde") {
return "z";
};
if (haireyes == "dirty blonde") {
return "b";
};
if (haireyes == "auburn") {
return "c";
};
if (haireyes == "brown") {
return "d";
};
if (haireyes == "black") {
return "e";
};
if (haireyes == "red") {
return "f";
};
if (haireyes == "blue2") {
return "g";
};
if (haireyes == "green2") {
return "h";
};
if (haireyes == "hazel2") {
return "i";
};
if (haireyes == "brown2") {
return "j";
};
}
</script>{"type":"image\/jpeg","link":"http:\/\/scoutsamerica.com\/uploads\/485604_10201093620571706_1239548317_n_5119195.jpg","status":"success","id":281,"traveler":{"name":"485604_10201093620571706_1239548317_n.jpg","type":"image\/jpeg","tmp_name":"\/tmp\/phpX1qywo","error":0,"size":60368}}