CodeIgniter Forums

Full Version: Ajax & Class Output
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to give data through an output class
PHP Code:
$this->output
  
->set_content_type('application/json''UTF-8')
 
 ->set_output(json_encode($my_array)); 
[Image: 4f1b6e667fbc98e8b722e1560c8b37f8.png]
As a result I get problems with Ajax
Code:
VM1166:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
   at JSON.parse (<anonymous>)
   at Object.<anonymous> (main.js:260)
   at i (jquery.min.js:2)
   at Object.fireWith [as resolveWith] (jquery.min.js:2)
   at A (jquery.min.js:4)
   at XMLHttpRequest.<anonymous> (jquery.min.js:4)
If I do through the echo...
[Image: 1760d5bf2532194686c928585145b990.png]
I do not understand where the pre  Huh
That's just auto generated HTML by Google Chrome.
You need to open the page that got your AJAX request, and there open up Network tab. Select your /json/ url and select Response.

That's the real response from the server.
If you look at the network tab, then the responses from the server are identical.


{"html":"","stop":"false","start":0} - This output class

{"html":"","stop":"false","start":0} - This echo

It is visible even that the text differs.
How are you handling the response from the ajax call? Please show the javascript and the portion of html is manipulates.
Code:
    $.ajax({
            url: "ajax/ajaxpag",
            method: "POST",
            data: post_data
        }).done(function(data) {
            var arr = JSON.parse(data);
            });
If you specify the content type as set_content_type(text/html) then everything works.
You need to specify that you want json and ignore parsing.

Code:
$.ajax({
  url: "ajax/ajaxpag",
  method: "POST",
  dataType: "json",
  data: post_data
}).done(function(data) {
  console.log(data);
  console.log(data.html);
});
Yes, it worked, but I had to remove the line
Code:
JSON.parse(data);

In the "callback" is returned already an array.
Thank you ! It's decided.