help syntaxError: unexpected token {in json at position 4

3

I do not know where the error is, but if you make the query well

$.ajax({
        url: base_url + 'cargar/carga',
        type : "POST",
        data: {id:id},   
        success: function(json,textStatus, jqXHR) {  
          console.log(json);

            if (json.Status==true)
            {      

              $('#nombre').val(json.Data.nombre);
              $('#apellido').val(json.Data.apellido);
              $('#tel').val(json.Data.tel);
              $('#domicilio').val(json.Data.domicilio);

              $('#modal_persona').modal('show');
          console.log("Succs");
      }
      else{console.log("error");
      }
        },
        complete : function(jqXHR , textStatus) {

        },

        error : function(jqXHR, textStatus, errorThrown) {
          console.log("complete error "+textStatus);
          console.log(jqXHR);
          console.log(errorThrown);
        },
        statusCode: {
             404: function() {
                  console.error( "page not found" );


             },
             500: function(){
                 console.log("Internal server error"); 
             }
         }

    });

controller

$id=$_POST['id'];
    print_r($id);
    $data = $this->model->carga($id);
    if ($data) {

                    $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        "Status" => true,
                        "Message" => "GET",
                        "Data" =>  $data
                    )));
                }else
                {
                     $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(500)
                    ->set_output(json_encode(array(
                        "Status" => false,
                        "Message" => "GET"

                    )));
                }
    
asked by Juan Jose 24.10.2018 в 02:03
source

1 answer

2

When you send an Ajax request that expects a JSON like in this case, you can not leave nothing but a JSON screen.

In the controller you have a code that will annoy you the exit:

print_r($id);

You must delete it.

Also, you can improve the Ajax request:

$.ajax({
        url: base_url + 'cargar/carga',
        method : "POST",
        dataType: "json",
        data: {id:id},   
        success: function(json,textStatus, jqXHR) {  
          console.log(json);

            if (json.Status)
            {      

              $('#nombre').val(json.Data.nombre);
              //resto del código

It is recommended to use done instead of success and fail instead of error . In the future success and error will be taken from the core of jQuery, so you are using code declared obsolete from jQuery 3.

    
answered by 24.10.2018 / 02:42
source