[英]How to indirect call php file by ajax with post type and get json return then parse it to table
[英]How to get response in ajax call of Json type in php?
我無法獲得成功的Ajax調用的數據。成功的數據仍然為空。 我該如何獲取數據以進行響應.......................................... ................................................... ................................................... ................................................... ....
report.blade.php
$.ajax({
type : "POST",
url : '{{ url('filter_reports') }}',
dataType: 'json',
data : {
'hours_from' : hours_from,
'hours_to' : hours_to,
'ddl_promotion' : ddl_promotion,
'ddl_shops' : ddl_shops,
'ddl_age_from' : ddl_age_from,
'ddl_age_to' : ddl_age_to,
'from_date' : from_date,
'to_date' : to_date,
'ddl_mall_center' : ddl_mall_center,
'ddl_postal_code' : ddl_postal_code,
'ddl_gender' : ddl_gender,
'ddl_report_type' : ddl_report_type,
'ddl_report_graph' : ddl_report_graph,
'_token' : '{{ csrf_token() }}'
},
success : function(data)
{
if((ddl_report_type == 1 || ddl_report_type == 3 || ddl_report_type == 6 || ddl_report_type == 7) && ddl_report_graph == 1)
{
$('#myChart').replaceWith('<canvas id="myChart" width="100" height="50"></canvas>');
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: **data.x,**
datasets: [{
label: 'Participants',
data: **data.y,**
backgroundColor: 'rgba(75,192,192,0.4)',
borderColor: 'rgba(75,192,192,1)',
borderWidth: 2
}]
},
options: {
title: {
display: true,
text: 'Participants'
},
scales: {
xAxes: [{
gridLines: {
display: false
}
}],
yAxes: [{
gridLines: {
display: false
},
ticks: {
beginAtZero:true,
min:**data.min,**
max:**data.max**
}
}]
}
}
});
}
ReportsController.php
if($report_graph == 1 || $report_graph == 2 || $report_graph == 3)
{
$get_data = DB::select("SELECT p.p_id,p.name,c.age FROM tbl_capture_img AS cap JOIN tbl_customers AS c ON c.c_id = cap.c_id JOIN tbl_promotions AS p ON p.p_id = cap.p_id WHERE cap.p_id = '{$promotion}'");
$agerange = array('18-30','31-40','41-50','51-60','61-70','71-80','81-90','91-100');
$final_age_range = '['.implode(',',$agerange).']';
$agecount = array(0,0,0,0,0,0,0,0);
foreach($get_data as $data)
{
if($data->age >=18 && $data->age <= 30)
{
$agecount[0] += 1;
}
else if($data->age >=31 && $data->age <= 40)
{
$agecount[1] += 1;
}
else if($data->age >=41 && $data->age <= 50)
{
$agecount[2] += 1;
}
else if($data->age >=51 && $data->age <= 60)
{
$agecount[3] += 1;
}
else if($data->age >=61 && $data->age <= 70)
{
$agecount[4] += 1;
}
else if($data->age >=71 && $data->age <= 80)
{
$agecount[5] += 1;
}
else if($data->age >=81 && $data->age <= 90)
{
$agecount[6] += 1;
}
else if($data->age >=91 && $data->age <= 100)
{
$agecount[7] += 1;
}
}
$final_age_count = '['.implode(',',$agecount).']';
$final['x'] = $final_age_range;
$final['y'] = $final_age_count;
$final['min'] = 0;
$final['max'] = max($agecount) + 5;
echo json_encode($final);
}
輸出量
{"x":"[18-30,31-40,41-50,51-60,61-70,71-80,81-90,91-100]","y":"[4,0,0,0,0,0,0,0]","min":0,"max":9}
但是我無法在data.x,data.y,data.min,data.max中獲得數組,因此圖形保持空白。 我該怎么辦?
在這種情況下,您需要對以下代碼進行故障排除:
首先檢查,使用console.log(),Ajax響應正確(甚至是json):
success : function(data)
{
console.log(data);
如果它具有您期望的值,請確保您使用block和alert()或類似的console.log()運行哪個塊。
if((ddl_report_type == 1 || ddl_report_type == 3 || ddl_report_type == 6 || ddl_report_type == 7) && ddl_report_graph == 1)
{
alert("hi"); or console.log("hi");
如果一切正常,則您將無法訪問標簽中的數據: data.x,並且需要定義一個全局變量,並在訪問后使用該變量:
success : function(data)
{
var gData = data;
並使用:
labels: **gData.x,**
如果每個步驟都被拒絕,則需要解決此問題,例如,如果數據沒有任何價值,則Laravel PHP Controller或通信結構中存在問題。
也有可能以字符串形式獲取數據,並且您需要在JavaScript中將其作為json解析(var parsedObjext = JSON.parse(data);)。
在您的report.blade.php文件中:在成功的ajax內添加以下行,如下所示:
....
success : function(data)
{
data = jQuery.parseJSON(data);
/*Add this above line and you can then access data.x and data.y*/
if((ddl_report_type == 1 || ddl_report_type == 3 || ddl_report_type == 6 || ddl_report_type == 7) && ddl_report_graph == 1)
{
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.