#1
  1. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,534
    Rep Power
    221

    Accessing Data With Jason


    Hi;

    When the response is an error, it doesn't alert or console.

    When it's success it workes well.

    What did I miss? Why isn't error working?

    "upload" controller that generates Json response:
    PHP Code:
    <?php
    defined
    ('BASEPATH') OR exit('No direct script access allowed');

    class 
    Upload extends CI_Controller {

        public function 
    index()
            {
                
                
                
    $config['upload_path']          = './uploads/';
                
    $config['file_name']            = $_POST['plc_id'].".jpg";
                
    $config['allowed_types']        = 'gif|jpg|png';
                
    $config['max_size']             = 1000;
                
    $config['max_width']            = 200;
                
    $config['max_height']           = 200;

                
    $this->load->library('upload'$config);
                if(!
    $this->upload->do_upload('userfile'))
                    {
                        echo 
    json_encode(array("errors"=>$this->upload->display_errors()));
                    }
                else
                    {
                        echo 
    json_encode(array("plc_id"=>$_POST['plc_id'],'file'=>$config['file_name']));
                    }    
            }
    }
    Jquery Ajax:
    Code:
    $(document).ready(function (e) {
    
        $('.plc_upload_thumbnail').on('submit',(function(e) {
            e.preventDefault();
            var formData = new FormData(this);
    
            $.ajax({
                type:'POST',
                url: '<?php echo base_url('upload');?>',
                data:formData,
                cache:false,
                contentType: false,
                processData: false,
                success:function(data){
                    var obj = JSON.parse(data);
                    var plc_id = obj.plc_id;
                    var file = obj.file;
                    $("#plc_thumb_"+plc_id).attr("src","/uploads/"+file);
                },
                error: function(data){
                   alert(data);
                    
                }
            });
        }));
    
        $("#ImageBrowse").on("change", function() {
            $("#imageUploadForm").submit();
        });
    });
  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,795
    Rep Power
    9646
    The error callback applies to when there was an error doing the AJAX request. jQuery doesn't know that an "errors" key means there was an error, it only cares about response codes and stuff like that. So that error callback should just about never happen.

    To start you off,
    Code:
    if (data.errors) {
  4. #3
  5. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,263
    Rep Power
    4193
    Alternatively, if you want to use the errors callback, change your PHP code to respond with an HTTP error code (4xx or 5xx) along with your errors array.

    If I recall correctly, the argument passed to the errors callback is the XHR object, not the response data so you'd have to grab and parse it yourself.
    Code:
    error: function(xhr){
        var data = JSON.parse(xhr.responseText);
        console.log(data.errors);
    }
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  6. #4
  7. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,534
    Rep Power
    221
    Sweet thanks

IMN logo majestic logo threadwatch logo seochat tools logo