Welcome Guest, Not a member yet? Register   Sign In
passing a string to a view
#13

(This post was last modified: 01-28-2018, 06:57 PM by richb201.)

I am still having an issue with this. Here is the controller called 'Configure' code

     //  $string2=json_encode($query->result());           //not using $string2 in this test
       $this->load->view('configure_chart.php.old');


   public function snd_json()  //this gets the request of the ajax in js
{
       $this->output->set_content_type('application/json', 'utf-8');
       $this->output->set_output(file_get_contents("sampleData.json"));
}

and here is the js part of the view

   function drawChart() {
       var jsonData = $.ajax({
           url: "<?php echo base_url(); ?>" + "index.php/Configure/snd_json",
           dataType: "json",
        success: function(jsonData)
        {
        var data = new google.visualization.DataTable(jsonData);
       var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));
       // Draw the chart, setting the allowHtml option to true for the tooltips.
      chart.draw(data, {allowHtml: true});
      },
      error: function(jsonData)
      {
      $('info').html('<p>An error has occurred</p>');
      },

            async: false
       }).responseText;

 
I can see that the snd_json is firing and I also see that error: is running. I think the error is due to the jsonData.responseText field that shows up at the client is the html text of a web page instead of the json data I sent over. I keep getting a parser error from AJAX since a regular HTML page is not JSON. I can see that I am sending the right data because I get it with get_output (just to check). I am just fustrated that AJAX is receiving the wrong data in the client.

Any ideas?

I downlowed Fiddler to see exactly what is being sent by my php "callback", snd_json. My message from snd_json appears in Fiddler as a return code 200 which is all is AOK. The content type is text/html. But as you can see in the code above I am setting it to 'application/json'.  It seems that my json data is getting appended onto the html (which my menu header stuff) and then being sent, rather than being sent all on its own. Of course when AJAX gets this HTML+json it gives a parser error because it expects json and the HTML part is not valid JSON. What I don't get is why the HTML is being included at all in what is being sent. It's not there when I look at the buffer prior to sending. But it is there according to Fiddler, when it "hits the cable" (that is just a saying).

I also tried using just echo instead of set output. In this case the Json  is first in the buffer, and then the rest of the HTML page is appended onto the end. Same issue with parsererror.
proof that an old dog can learn new tricks
Reply


Messages In This Thread
passing a string to a view - by richb201 - 01-17-2018, 11:12 PM
RE: passing a string to a view - by jreklund - 01-18-2018, 02:09 AM
RE: passing a string to a view - by InsiteFX - 01-18-2018, 04:37 AM
RE: passing a string to a view - by richb201 - 01-18-2018, 09:52 AM
RE: passing a string to a view - by richb201 - 01-18-2018, 12:08 PM
RE: passing a string to a view - by jreklund - 01-18-2018, 12:21 PM
RE: passing a string to a view - by richb201 - 01-18-2018, 02:48 PM
RE: passing a string to a view - by jreklund - 01-18-2018, 03:17 PM
RE: passing a string to a view - by richb201 - 01-18-2018, 03:47 PM
RE: passing a string to a view - by jreklund - 01-19-2018, 12:53 AM
RE: passing a string to a view - by richb201 - 01-19-2018, 01:35 AM
RE: passing a string to a view - by jreklund - 01-19-2018, 04:37 AM
RE: passing a string to a view - by richb201 - 01-26-2018, 11:49 PM
RE: passing a string to a view - by richb201 - 01-28-2018, 09:33 PM



Theme © iAndrew 2016 - Forum software by © MyBB