You can just return an array of the data and laravel will convert it to json. Or use api resources
how to pass json arrays as arguments in ajax call in laravel blade file
i want to send a response as argument in ajax call to jquery highchart here is code
function my_chart(response) {
$('#container').highcharts('StockChart', {
series: [{
type: 'candlestick',
id: 'aapl',
name: 'AAPL Stock Price',
data: response
}]
});
}
i make argument for response that will get from ajax call
chart();
function chart() {
$.ajax({
type: "GET",
url: "/candle",
dataType: "json",
success: function(response) {
console.log(response);
let parsed_response = (response);
my_chart(parsed_response);
}
});
}
need to pass json response that get from laravel controller as
0: Object { id: 1, openprice: "0.0009", closeprice: "0.0010", … }
1: Object { id: 17, openprice: "0.0010", closeprice: "0.0029", … }
2: Object { id: 18, openprice: "0.0029", closeprice: "0.0020", … }
3: Object { id: 19, openprice: "0.0020", closeprice: "0.0010", … }
4: Object { id: 20, openprice: "0.0010", closeprice: "0.0015", … }
5: Object { id: 21, openprice: "0.0015", closeprice: "0.0017", … }
this json response array need to pass as argument in here
success: function(response) {
console.log(response.priceall);
let parsed_response = jQuery.parseJSON(response.priceall);
my_chart(parsed_response);
}
how can pass this json arrays in parsed_response variable ,
this json array will assign as argument response
function my_chart(response) {
$('#container').highcharts('StockChart', {
series: [{
type: 'candlestick',
id: 'aapl',
name: 'AAPL Stock Price',
data: response
}]
});
}
in data : response as
data : []
when i pass as
success: function(response){
//call my_chart function
console.log(response.priceall);
let parsed_response = jQuery.parseJSON(response.priceall);
my_chart(parsed_response);
},
show error
Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
why this happen i not know
can someone help me .
@zwarkyaw Does Highcharts request a specific structure on the data variable? if not you could just pass response.priceall . In any other occasion just parse with foreach the response.priceall table and create the respective arrays to pass to my_chart. I would go on with normalizing the array on the controller but this approach will work too.
Please or to participate in this conversation.