当我尝试从我的LTS服务器(不在我的本地计算机)上发送带有AJAX的FormData时似乎存在问题。我怀疑FormData并不支持所有的浏览器,但它可以在我的本地机器上完美地工作,使用我在我的服务器(LTS)上试用过的相同浏览器。我在这里有点迷路,不知道该怎么做。如何通过AJAX正确发送FormData()
为了进一步解释:
指数
<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控制器的响应,我认为这不响应。
尝试从ajax请求中删除网址中的点,然后重试。 –
我没有看到我的控制器从URL的目录有任何问题。当'data:data'没有通过时,它工作正常 –