Jquery AJAX responseText返回未定义的
我试图使用ajax将一个图像上传到服务器上,它成功了,现在的问题是从ajax返回数据,它继续使用ResContext提供未定义的数据Jquery AJAX responseText返回未定义的,jquery,ajax,codeigniter,Jquery,Ajax,Codeigniter,我试图使用ajax将一个图像上传到服务器上,它成功了,现在的问题是从ajax返回数据,它继续使用ResContext提供未定义的数据 $('#imageform').submit(function(e){ var formData = new FormData(this); var request = $.ajax({ url: "<? echo site_url('mybazaar/uploadProductImage'); ?>", type: "POST"
$('#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();
});
$('#imageform')。提交(函数(e){
var formData=新formData(本);
var请求=$.ajax({
url:“”,
类型:“POST”,
数据:formData,
contentType:false,
processData:false,
成功:功能(res){
var newImage=$(res).filter('#ajaximage').html();
$('ajax#product_image')。追加(newImage);
警报(res.responseText);
},
错误:功能(请求、状态、错误){
警报(错误);
}
}).done(函数(){
});
e、 预防默认值();
});
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>";
}
}
public函数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);
如果(!$this->upload->do_upload(“产品图片主”)){
返回false;
}
否则{
$data=array('upload_data'=>$this->upload->data());
$this->resize($data['upload_data']['full_path']);
回声“;
}
}
但是,如果我只执行“alert(res)”,整个页面的HTML都会收到警报,包括我在“mybazaar/uploadProductImage”中回显的文本
顺便说一句,我使用的是Jquery 1.9和CodeIgniter。回调成功的第一个参数是以content type header指定的格式从服务器返回的实际数据,默认为html,在您的情况下也是如此。现在您已经恢复了html,您不必使用
filter('filter')搜索div#ajaximage')
因为这正是您在res
中拥有的(div#ajaximage),所以您可以直接将其附加到DOM中
success: function (res) {
$('#ajax_product_image').append(res);
}
我删除了
alert(res.responseText)
,因为responseText
不是成功回调的数据参数中定义的属性,它是用于触发Ajax调用的实际XMLHttpRequest
对象的属性。如果您仍然需要访问responseText,则成功回调有3个参数函数(数据、状态、jqXHR)。jqXHR(基本上是第三个参数,如果您愿意,您可以更改名称)将具有原始对象的所有基本属性
XMLHttpRequest`object请向我们展示您的php文件以获得更好的帮助您希望res.responseText
包含哪些内容?使用php文件编辑,res.responseText应该包含在我的PHP文件中回显的数据。但是你刚才说res
包含数据。请注意,CodeIgniter最有可能将响应放在页面模板中。Yes res包含呈现的其他页面数据,但是res.responseText返回undefined。我尝试了filter(“#ajaximage”).html(),也返回了undefined。您好,如果我有多个div,并且希望将它们附加到页面的不同位置,该怎么办?我以前在其他项目中使用过该代码,没有任何问题$(res).filter(“#idtobefiltered”).html(),但它在codeigniter中似乎不起作用。然后将其包含在另一个div中。现在res将是您的外部div,而您的过滤器将与通常包含在“ajaximage”div中的过滤器一样工作,如果对它们进行过滤,我将变得未定义。不,我的意思是说将您的代码包含在另一个div中“ajaximage”div在另一个div中。现在你应该在res n中获得你的新div,并对其进行过滤,这样应该可以工作