Codignitor ajax within ajax, controller not receiving data

Hi guys,

CI Version 3

I am having a very very hard time finding the bug.

<script>
$(document).delegate("#btn_upload", "click", function(event){
        

        var fd = new FormData();
        var files = $('#file')[0].files;
        var product_id=$("#product_id").val();
        
        // Check file selected or not
        if(files.length > 0 ){
           fd.append('file',files[0]);
           fd.append('product_id',product_id);
           //alert(fd);

           $.ajax({
              url: '<?php echo base_url(); ?>products/photo_upload',
              type: 'POST',
              data: fd,
              processData: false, 
              contentType: false,              

 
			  beforeSend:function(){
				 $("#loader_upload").show();
			  },

			  complete:function(){
				 $("#loader_upload").hide();
			  }, 
		 
              success:function(response){
                     $('#alert_upload').html(response);
                     
                     //if file upload is success. then write to db//
                     if(response!=""){
                          var dataString = 'product_id='+ product_id
                    					 + '&file_name=' + response;
                          //alert(dataString); 
                          
                          $.ajax({
                              url: '<?php echo base_url(); ?>products/update_photo_path_to_db',
                              type: 'POST',
                              data: dataString,
                              contentType: false,
                              processData: false,                              


                			  beforeSend:function(){
                				 $("#loader_upload").show();
                			  },
                
                			  complete:function(){
                				 $("#loader_upload").hide();
                			  },


                              success: function(response){
                                 console.log(response);
                                 $('#alert_upload').html(response);
                                 if(response=="1"){
                                    //db update success    
                                 }else{
                                    alert('Opps. coult not update database.Please try again');  
                                    return false;
                                 }
                              },
                           });                    
                        
                     }else{
                        alert('Photo upload error.');  
                        return false;
                     }
                     
                     
                     
              },
              
           });
        }else{
           alert("Please select a file.");
        }
});    
</script>

Console > network > Headers

product_id=22&file_name=/home/likesril/ibfresh.domain.xyz/assets/uploads/22_user2-160x160.jpg

CI Controller

    public function update_photo_path_to_db() {
        
            var_dump($this->input->post());

var dump out put : array(0) { }

Type as alias of method correct…

if you’re using versions of jQuery prior to 1.9.0.

jQuery.ajax() | jQuery API Documentation

i am sorry, i did not undestand your point here.

You use in your AJAX request attribute type for HTTP-method. If your JQuery version not prior to 1.9.0., that is not correct. That’s why you will get default HTTP-method GET and empty POST.

i am using jQuery v3.4.1
i have little doubt here,
you see header params are is sent as a continues string

but a successful one for example shows like this

image

Is that request body, or query string?

What your console tells about HTTP-method of this request?

Try to dump on server side $_GET, $_REQUEST and HTTP-method.

i see data such as

Request Method: POST
content-type: text/html; charset=UTF-8
x-requested-with: XMLHttpRequest

Okey, What is in $_GET and $_REQUEST on server side?

Another one thing… Probably problem in URL-encoding. Try to encode your file name:
encodeURIComponent() - JavaScript | MDN (mozilla.org)

sorry did not help.

                	 var dataString = 'product_id='+ product_id 
                					+ '&file_name=' + encodeURIComponent(response);

image

Thank you igor_g for all your efforts to solve my problem.
i had posted this question in CI forum also. just got a reply there and problem is solved now.

this what solved the issue

CI Forum Reply : Try to remove contentType: false, from second request

cheers.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.