0

i'm tying to do an ajax get call i can successfully console.log the result without putting the datatype json in the ajax

    dataType: 'json',

when i console.log without it i get

{"id":"1","post_id":"748037498494861","post_title":"laptop","image1":"team217.jpg","price":"1234"}{"id":"2","post_id":"740811329642473","post_title":"remote control car","image1":"team522.jpg","price":"50"}{"id":"4","post_id":"316194613858174","post_title":"Ipad 3","image1":"team523.jpg","price":"400"}

however i cant display the json data if i put

     dataType: 'json',

my

      console.log

is empty i dont understand where the problem is

    $(document).ready(function(){
var username = $("#usernameinfo").text();

$.ajax({
    type:"GET",


    url: "<?= base_url()?>"+"account/listings/more_user_ads",
    data: {"username":username,"pid":"<?=$this->input->get('pid')?>"},
    success: function(res){
      console.log(res);   

}


});

});

php

 function more_user_ads(){
$post_id = $this->input->get('pid');

$username = $this->input->get('username');
$query = $this->get_where_custom('username', $username);
if($query->num_rows()>0){
    foreach($query->result() as $row){
        if($post_id != $row->post_id){
       $result = array(
           'id'=> $row->id,
           'post_id'=> $row->post_id,
           'post_title'=> $row->post_title,
           'image1'=> $row->image1,
           'price'=> $row->price,
           'price'=> $row->price,

       );

       $res = json_encode($result);

        echo $res;
7
  • 1
    That is not valid JSON, you don't echo json_encode calls in a loop Commented Apr 19, 2018 at 21:49
  • 2
    Run your string through: jsonlint.com. It's not valid json so your ajax fails. Commented Apr 19, 2018 at 21:49
  • 2
    My guess is your PHP needs to run header("Content-type: application/json");; right now it's sending plain text that happens to be JSON. Also, you need to do $res[] = $result; inside the loop, then call echo json_encode($res); once, after the loop. Commented Apr 19, 2018 at 21:50
  • thanks for the info on putting the result as an array it does console.log a different outcome however it still doesnt allow the datatype: 'json' thats with the hears changed Commented Apr 19, 2018 at 22:16
  • also retested the new json in the above link it still gives an error on line 7 ??? Commented Apr 19, 2018 at 22:17

1 Answer 1

1

Add each row to the $result array then echo the json_encode once.

public function more_user_ads()
{
    $post_id  = $this->input->get('pid');
    $username = $this->input->get('username');
    $query    = $this->get_where_custom('username', $username);

    $result = []; //so we have something if there are no rows
    if($query->num_rows() > 0)
    {
        foreach($query->result() as $row)
        {
            if($post_id != $row->post_id)
            {
                $result[] = array(
                    'id'         => $row->id,
                    'post_id'    => $row->post_id,
                    'post_title' => $row->post_title,
                    'image1'     => $row->image1,
                    'price'      => $row->price,
                    'price'      => $row->price,
                );
            }
        }
    }
    echo json_encode($result);
}

Actually, you can shorten this a bit by using $query->result_array(); because you won't have to convert an object to an array.

public function more_user_ads()
{
    $post_id  = $this->input->get('pid');
    $username = $this->input->get('username');
    $query    = $this->get_where_custom('username', $username);

    $result = []; //so we have something if there are no rows
    if($query->num_rows() > 0)
    {
        $rows = $query->result_array();
        foreach($rows as $row)
        {
            if($post_id != $row['post_id'])
            {
                $result[] = $row;
            }
        }
    }
    echo json_encode($result);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.