Jquery AJAX 响应文本 returns 未定义
Jquery AJAX responseText returns undefined
我正在尝试使用 ajax 将图像上传到服务器并且成功了,现在问题是从 AJAX 返回数据,它一直在使用 responText 给出未定义。
$('#imageform').submit(function(e){
var formData = new FormData(this);
var request = $.ajax({
url: "<? echo site_url('mybazaar/uploadProductImage'); ?>",
type: "POST",
data: formData,
contentType: false,
processData: false,
success: function (res) {
var newImage = $(res).filter('#ajaximage').html();
$('#ajax_product_image').append(newImage);
alert(res.responseText);
},
error: function (request, status, error){
alert(error);
}
}).done(function(){
});
e.preventDefault();
});
PHP 文件:
public function uploadProductImage(){
$config['upload_path'] = './images/product_images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10024;
$config['max_width'] = 10240;
$config['max_height'] = 10240;
$upload_path = 'images/product_images/';
$this->load->library('upload', $config);
if (!$this->upload->do_upload("product_image_primary")){
return false;
}
else{
$data = array('upload_data' => $this->upload->data());
$this->resize($data['upload_data']['full_path']);
echo "<div id='ajaximage'><div class='col-md-2'><img src='".base_url().$upload_path.$data['upload_data']['file_name']."'></div></div>";
}
}
但是,如果我只做 "alert(res)",整个页面的 HTML 都会收到警报,包括我在 "mybazaar/uploadProductImage" 中回显的文本。
我正在使用 Jquery 1.9 和 CodeIgniter。
success
回调的第一个参数是服务器返回的实际数据,格式为content-typeheader,默认为html,也在你的情况下。现在您正在返回 html,您不必使用 filter('#ajaximage')
搜索 div
因为那正是 (div#ajaximage) 你在 res
中所拥有的,所以你可以直接将其附加到你的 DOM.
success: function (res) {
$('#ajax_product_image').append(res);
}
我删除了 alert(res.responseText)
因为 responseText
不是在成功回调的数据参数中定义的 属性,它是实际 XMLHttpRequest
的 属性 object 用于触发 Ajax 调用。如果您仍然需要访问 responseText,成功回调有 3 个参数 function(data, status, jqXHR). jqXHR(3rd argument basically, you can change the name if you like) will have all the underlying properties of original
XMLHttpRequest` object
我正在尝试使用 ajax 将图像上传到服务器并且成功了,现在问题是从 AJAX 返回数据,它一直在使用 responText 给出未定义。
$('#imageform').submit(function(e){
var formData = new FormData(this);
var request = $.ajax({
url: "<? echo site_url('mybazaar/uploadProductImage'); ?>",
type: "POST",
data: formData,
contentType: false,
processData: false,
success: function (res) {
var newImage = $(res).filter('#ajaximage').html();
$('#ajax_product_image').append(newImage);
alert(res.responseText);
},
error: function (request, status, error){
alert(error);
}
}).done(function(){
});
e.preventDefault();
});
PHP 文件:
public function uploadProductImage(){
$config['upload_path'] = './images/product_images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10024;
$config['max_width'] = 10240;
$config['max_height'] = 10240;
$upload_path = 'images/product_images/';
$this->load->library('upload', $config);
if (!$this->upload->do_upload("product_image_primary")){
return false;
}
else{
$data = array('upload_data' => $this->upload->data());
$this->resize($data['upload_data']['full_path']);
echo "<div id='ajaximage'><div class='col-md-2'><img src='".base_url().$upload_path.$data['upload_data']['file_name']."'></div></div>";
}
}
但是,如果我只做 "alert(res)",整个页面的 HTML 都会收到警报,包括我在 "mybazaar/uploadProductImage" 中回显的文本。
我正在使用 Jquery 1.9 和 CodeIgniter。
success
回调的第一个参数是服务器返回的实际数据,格式为content-typeheader,默认为html,也在你的情况下。现在您正在返回 html,您不必使用 filter('#ajaximage')
搜索 div
因为那正是 (div#ajaximage) 你在 res
中所拥有的,所以你可以直接将其附加到你的 DOM.
success: function (res) {
$('#ajax_product_image').append(res);
}
我删除了 alert(res.responseText)
因为 responseText
不是在成功回调的数据参数中定义的 属性,它是实际 XMLHttpRequest
的 属性 object 用于触发 Ajax 调用。如果您仍然需要访问 responseText,成功回调有 3 个参数 function(data, status, jqXHR). jqXHR(3rd argument basically, you can change the name if you like) will have all the underlying properties of original
XMLHttpRequest` object