Welcome Guest, Not a member yet? Register   Sign In
Ajax & Class Output
#1

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
Reply
#2

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.
Reply
#3

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.
Reply
#4

How are you handling the response from the ajax call? Please show the javascript and the portion of html is manipulates.
Reply
#5

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.
Reply
#6

(This post was last modified: 01-21-2018, 09:57 AM by jreklund.)

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);
});
Reply
#7

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB