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.
Add
header('content-type: application/json');
to your controller (before the echo) to set the correct mime-type for you application's response.
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);
}
Related
I'm trying to return a query made in a controller to the view file so I can use that data in my form. But I am unable to successfully return the data without errors. I know the function is working because it returns the right data but has an error.
Here is my CustomersController fill function which is running the sql query.
public function fill(){
$layout = 'ajax';
$this->autoRender = false;
if ($this->request->is('post')) {
$id = $this->request->data['id'];
$query = $this->Customers->find()
->where([
'id' => $id
])->first();
echo json_encode($query);
}
}
and here is my blah.ctp which is the view file.
<?php use Cake\Routing\Router; ?>
<?= $this->Form->create(Null, ['type' => 'POST']) ?>
<?php
echo $this->Form->input('customer_id', ['options' => $customers, 'empty' => true,'id'=>'customers']);
?>
<?= $this->Form->end() ?>
<script>
document.getElementById('customers').addEventListener('change',function(){
var id = this.value;
var csrfToken = $('[name=_csrfToken]').val();
$.ajax({
type: "POST",
url: '<?php echo Router::url(array("controller" => "Customers", "action" => "fill")); ?>',
data: {'id' : id},
beforeSend: function(xhr){
xhr.setRequestHeader('X-CSRF-Token', csrfToken);
},
success: function(data){
alert(data);
}
});
});
</script>
Currently this is what happens when I select a customer in my drop down box which triggers the script in the view.
As you can see it returns the array of data I need but also has the error cannot emit headers. I have tried solving this error following other questions on stack overflow but can't solve it.
I've tried using $this->set instead of echo json_encode but it always returns nothing. I'm not sure what other way to do this.
First of all, If you're selecting a single record by a unique ID, you can call
->get($id) on the table object directly, instead of building a query chain with ->find().
CakePHP 3.x should automatically handle converting your view to JSON by using the RequestHandlerComponent. Typically, you must enable it if your scaffolding or installation didn't.
1) Enable request handler component. (If not already enabled) https://book.cakephp.org/3.0/en/controllers/components/request-handling.html
2) Remove the echo json_encode($query) line; you should not echo anything as this will break the request/response cycle. This is the cause of your error.
Instead, you should serialize your data to the view. Assuming you have the fetched data in $data: $this->set(compact('data')). Then, make sure you add $this->set('_serialize', ['data']) (again, assuming the data is stored in variable name 'data').
3) Reference this doc for information on how you can request the json. You can use a file extension (.json, .xml).
Also, make sure you add the 'Accept: application/json' header to your request.(https://book.cakephp.org/3.0/en/development/routing.html#Cake\Routing\Router::extensions).
I apologize for the fuzzy response. There are many ways to achieve this with CakePHP3. Please see this doc page for more information: https://book.cakephp.org/3.0/en/views/json-and-xml-views.html
For my project I try to create an ajax call and handle the data.
My Ajax call:
$.ajax({
type: 'GET',
dataType: "json",
url: ****,
data: {****},
success: function(data){
console.log(data.rooms);
$('.pagination').html(data.paginate);
},
error: function(){
alert('failure');
}
});
On the server with laravel I create a response with:
return response()->json(['rooms' => $rooms->toJson() , 'paginate' => $bathrooms->render()]);
In my ajax call the json response looks like (Edit this is the original json):
{"rooms": "{\"total\":3,\"per_page\":9,\"current_page\":1,\"last_page\":1,\"next_page_url\":null,\"prev_page_url\":null,\"from\":1,\"to\":3,\"data\":[{\"id\":237,\"name\":\"Modern met allure\",\"description\":\"Badkamer blablabla\",\"collection_id\":187,\"style_id\":7,\"created_at\":\"-0001-11-30 00:00:00\",\"updated_at\":\"-0001-11-30 00:00:00\"},{\"id\":243,\"name\":\"TIjdloze charme\",\"description\":\"Tijdloze charme\",\"collection_id\":187,\"style_id\":2,\"created_at\":\"-0001-11-30 00:00:00\",\"updated_at\":\"-0001-11-30 00:00:00\"},{\"id\":245,\"name\":\"Staande badkraan Bollicine\",\"description\":\"blablabla\n\",\"collection_id\":199,\"style_id\":7,\"created_at\":\"-0001-11-30 00:00:00\",\"updated_at\":\"-0001-11-30 00:00:00\"}]}","paginate": ""}
Now I want to loop all the object in data so First I tried:
console.log(data.rooms);
this gave me the first part i wanted (only the rooms not the something_else, next I tried to get only the data part by
console.log(data.rooms.data);
But then the result is
undefined
How should I access/loop through the data (subpart of rooms).
I think the problem that you have two data here. One is variable name and second is key name.
Try to use either:
console.log(data.rooms["data"]);
Or choose another name for success argument:
success: function(roomsdata){
console.log(roomsdata.rooms);
$('.pagination').html(roomsdata.paginate);
},
UPDATE: on seeing how you generate json on server:
return response()
->json([
'rooms' => $rooms->toJson() ,
'paginate' => $bathrooms->render()
]);
You're doing double json encoding: first encoding $rooms to json with toJson() and then encoding json-string to json again with json(). Remove toJson() call, leave only json().
i have tag in cakephp like this:
var url_save = "<?php echo $this->Html->url(array('controller' => 'users','action' => 'save_template')) ;?>";
$.ajax({
url : url_save,
type : "POST",
data : JSON.stringify(templates),
dataType : 'json'
});
how the controller can take the data value?
You can use $this->request->data on the Controller the usual way.
you can get json data using $this->data in controller, you can check if the request is ajax if you want using $this->RequestHandler->isAjax() but you must also add RequestHandler in component variable.
I'm creating dynamic dropdown lists from values in my database. I'm querying the data, creating a php variable in this format:
$items = array(
'red'=>'apples,firetrucks,blood',
'yellow'=>'bus,pencil,duck'
);
I can then use the json_encode() function to encode.
{
"red":"apples,firetrucks,blood",
"yellow":"bus,pencil,duck",
}
I know the getJson is passed at file or a path like:
$.getJSON("jsondata/data.json", function(data) {
Can my json variable be passed to this function instead? The reason I want a variable to be passed, rather than a file is because my data can chance on a daily basis, ie:
$items = array(
'red'=>'apples,firetrucks,bricks',
'yellow'=>'bus,pencil,duck'
);
You need to create a PHP file that outputs the JSON you want;
In /foo.php:
echo json_encode(array(
"red" => "apples,firetrucks,blood",
"yellow" => "bus,pencil,duck",
));
... then your JavaScript should make an AJAX request to foo.php using getJSON;
$.getJSON("/foo.php", function (data) {
alert(data.red); // shows "apples,firetrucks,blood
alert(data.yellow); // you get the idea
});
... It was so tempting to make the values for your object arrays, but I resisted... somehow
Hi there I was wondering if somebody could help me?
I have the following code. It retrieves JSON data from a php file. The Json is the following format :
{"Title":"rose","Price":1.25,"Number":15},{"Title":"daisy","Price":0.75,"Number":25},{"Title":"orchid","Price":1.15,"Number":7}
This JSON is created using the following php code:
$shop = array();
$shop = array( array( Title => "rose",
Price => 1.25,
Number => 15
),
array( Title => "daisy",
Price => 0.75,
Number => 25,
),
array( Title => "orchid",
Price => 1.15,
Number => 7
)
);
echo json_encode($shop);
Whenever i try and access the data using obj.Title I get an undefined message.
$.ajax({
type: "GET",
url: "data.php",
success: jsonDo
});
//JSON DATA = {"Title":"rose","Price":1.25,"Number":15},{"Title":"daisy","Price":0.75,"Number":25},{"Title":"orchid","Price":1.15,"Number":7}
function jsonDo(data) {
var obj = jQuery.parseJSON(data);
alert(obj.Title)
}
I was wondering how I can access the keys in the JSON and display the data?
Thanks a million.
var obj = jQuery.parseJSON('{"Title":"rose","Price":"1.25","Number":"15"}');
alert(obj.Title);
This work. Check difference in your code.
OK this is more correct:
var obj = [
{"Title":"rose","Price":"1.25","Number":"15"},
{"Title":"daisy","Price":"0.75","Number":"25"},
{"Title":"orchid","Price":"1.15","Number":"7"}
];
alert(obj[1].Title);
You have to specify that you are expecting a JSON object by informing the dataType: "JSON" parameter to the ajax() function, so you will not have to parse the data.
There seems to be some PHP errors in your code. This could cause php to raise a notice / warning which might break the Json output and cause javascript to raise errors when trying to parse it.
The correct jSon output should have been
[{"Title":"rose","Price":1.25,"Number":15},{"Title":"daisy","Price":0.75,"Number":25},{"Title":"orchid","Price":1.15,"Number":7}]
Since it is in an array, the JS should be:
$.ajax({
type: "GET",
url: "data.php",
success: jsonDo
});
function jsonDo(data) {
var obj = jQuery.parseJSON(data);
alert(obj[0].Title)
}
You should just use jQuery's $.getJSON method:
$.getJSON('data.php',function(data) {
alert(obj.Title);
});
try obj[0]["Title"] or obj[0].Title
dont forget that you have nested a lot of arrays and that yoyu need to access them that way again.