1

In my Controller I've have an array $data whose var dump is as follows:

array
  'urls' =>
    array
      0 =>
        array
          'link_id' => string '1' (length=1)
          'link_name' => string 'http://www.nytimes.com' (length=22)
  'words' =>
    array
      0 =>
        array
          'keyword_id' => string '1' (length=1)
          'keyword' => string 'republican' (length=10)

Array Structure:

$ data will have urls and words only but they can have multiple values. Both will not have the same cardinality.

Then I encode it as echo json_encode($data); in displayData and send this to ajax. displayData needs no POST data. The ajax request made in the View is as follows:

$.ajax({
    url:"http://localhost/codeigniter/SiteController3/displayData",
    success: function(response){
        alert(response);
        $("#user_data").html(response);
    },
    dataType:"json"
})
  • I want to access the response in my 'View' so that I can perform json_decode($response, true) and get the associative array.
  • There is a chunk of code which renders this data in tabular format by looping on the array. And before this code I want to get the associative array.
  • I tried using $.getJSON instead of $.ajax but no solution. Also tried $.each function, but on alert only getting undefined. Did JSON.stringify which on alert displayed the JSON but not able to send it to PHP code.

EDIT:

@fragmentedreality's answer

The content-type inconsistency is solved using the answer. But how can I access the response received on success of AJAX in html body of my View which has a chunk of PHP code for displaying data in tabular format ?

Solution:

Check my answer below.

Community
  • 1
  • 1
SilentAssassin
  • 480
  • 1
  • 9
  • 26
  • What is the mime-type of your response? It has to be `application/json` (http://stackoverflow.com/a/477819/834309), otherwise ajax will not properly decode it (thus making response `undefined`). If it is the correct mime-type your response will already by a json-object. – fragmentedreality Feb 21 '13 at 11:17
  • @fragmentedreality Under Firebug, in Net tab in `GET displayData` the `Content-Type` in `Response Headers` is `text/html` and in `Request Headers` it is `application/json`. Why is it so ? I am performing the json_encode in my controller. – SilentAssassin Feb 21 '13 at 11:29
  • PHP does not automatically decide to send out the correct mime-type. You ccould configure your webserver to do so or set the expected mime-type manually. If you access files ending on .json I would let the webserver do the work. In your case just send the header from your controller (see [my answer](http://stackoverflow.com/a/15001314/834309)). – fragmentedreality Feb 21 '13 at 11:41
  • 1
    You can not access that data in PHP. AJAX is client-sided. The browser makes the request to your controller (via javascript) and get the response in return. PHP is processed on the server **before** sending out to the client. So you will have to process the received response in your success-callback. You can – of course – change your controller to generate the proper HTML in PHP and return that as part of the response (e.g. `response.html`) instead. – fragmentedreality Feb 21 '13 at 13:02

2 Answers2

1

Add

header('content-type: application/json');

to your controller (before the echo) to set the correct mime-type for you application's response.

fragmentedreality
  • 1,182
  • 9
  • 27
  • Okay that works. But how to access this response in the `html body` of the same page ? I've to send the response received on AJAX success to the the chunk of code in my `html body`. That is my **main issue**. – SilentAssassin Feb 21 '13 at 11:43
  • That depends on your HTML. Please provide more information. You said you have a piece of code, that processes your response. You won't get this chunk of data back to your PHP as the AJAX is requested from your client. So you can decide to gnerate the proper HTML in your controller and send that as a response (not much of a AJAX-approach) or write something in javascript that processes the received json-object and fills in the data in your page. – fragmentedreality Feb 21 '13 at 12:02
1

Finally I dropped the JSON approach. I added the following line in displayData method of the Controller:

$this->load->view('data_processing', $data);

The data_processing.php is a new View that generates the HTML table I wanted to display. The response of this View is loaded in my original View by the following AJAX request:

$.ajax
({
    url: "http://localhost/codeigniter/SiteController3/displayData",
}).done(function(data)
    {
         console.log(data);
         $('#user_data').html(data); 
    }
SilentAssassin
  • 480
  • 1
  • 9
  • 26