当我尝试从我的 LTS 服务器(而不是在我的本地计算机上)使用 AJAX 发送我的 FormData 时,似乎出现了问题。我怀疑并非所有浏览器都支持 FormData,但它确实在我的本地计算机上使用我在服务器 (LTS) 上尝试时使用的相同浏览器完美运行。我在这里有点迷茫,不知道该怎么办。
进一步解释:
指数
<form class="image" enctype="multipart/form-data" accept-charset="utf-8" method="post">
<input id="image-value" data-id="{{id}}" name="{{picture}}" class="image-box-{{id}}" data-type="{{type}}" type="file" value="{{picture}}" />
<p id="{{id}}" class="label" >Update</p>
</form>
我需要在此处获取文件并将其上传到服务器目录中
脚本
$('[id="image-value"]').change(function(e) {
var data = new FormData();
var file_data = this.files[0];
data.append('file', file_data);
e.preventDefault();
$.ajax({
url: './someController/upload_picture/',
type: 'POST',
data: data,
cache: false,
contentType: false,
processData: false,
dataType: 'json',
success: function(data){
if (data.success == true) {
console.log(data.image_name);
} else {
var error = data.error_message;
$(".question-message").fadeIn("fast").html(error);
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.error("The following error occured: " + textStatus, errorThrown);
}
});
});
控制器
function __construct() {
parent::__construct();
$config['upload_path'] = './data/picDir/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
}
function upload_picture() {
if ($this->check_authorization()) {
if ($this->ion_auth->get_user_id()) {
if ($this->upload->do_upload('file')) {
$data = $this->upload->data();
echo json_encode(array(
"image_name" => $data["file_name"],
"success" => true
));
} else {
echo json_encode(array(
"success" => false,
"error_message" => $this->upload->display_errors()
));
}
}
}
}
触发时,请求只是继续等待来自 PHP 控制器的响应,我认为该控制器没有响应。
我相信你的"回声json_encode..."应该在响应中 - 类似于 $this->响应(json_encode...echo 不会返回对 ajax 请求的响应。请检查您的框架或库的正确响应并分享它。