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 performjson_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 gettingundefined
. DidJSON.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.