gpt4 book ai didi

javascript - 将十进制字符串从 php 传递到 jquery

转载 作者:行者123 更新时间:2023-12-03 06:34:37 25 4
gpt4 key购买 nike

我正在尝试将一些格式化字符串从 PHP 传递到 jQuery。但我收到错误为

VM2203:3 Uncaught SyntaxError: Unexpected number in the browser.

这是我的代码

<?php
$OSlist = [];
$category = "";
$pieData = "";
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_PORT => "8081",
CURLOPT_URL => "http://localhost:8081/api/devices/hoursBy/OSVersion",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
$decodeStr = json_decode($response, true);
foreach ($decodeStr as $item) {
$os = $item['_id']['os'];
if ($os == $selectedOS) {
if ($item['total_hours'] != 0) {
$OSlist[$item['_id']['version']] = $item['total_hours'];
// echo 'iOS'.$item['_id']['version'] . '&nbsp;:&nbsp;' . $item['total_hours'] ,"<br>";
}
}
}
ksort($OSlist);
foreach ($OSlist as $key => $val) {
// echo "$key = $val" . "<br>";
$category = $category . $key . ",";
$pieData = $pieData . "[" . $key . "," . $val . "],";
}
$pieData="[".$pieData."]";
echo $pieData;
// print_r($OSlist);
} ?>
<script >
$('document').ready(function () {
var jsArray = <?php echo $pieData; ?>;
alert(jsArray);

});


</script>

Piedata 输出为

[[2.3.4,97.9],[4.0.3,11.6],[4.0.4,2.4],[4.1.1,15.1],[4.1.2,398.4],[4.2.1,45.8],[4.2.2,27.3],[4.3,321],[4.4.2,694.9],[4.4.4,478.7],[5,646.3],[5.0.1,177.5],[5.0.2,1210.5],[5.1,126.2],[5.1.1,524.4],[6,201.5],]

我实际上需要将这些数据传递到 highchart 饼图的数据值。下图是移动版本与其使用时间的关系。

function loadPieGraph() {
// Radialize the colors
var android = <?php echo $androidhrs ?>;
var ios = <?php echo $ioshrs ?>;
var obj = [{
type: 'pie',
name: 'Browser share',
data: [
['4.1', '23.0'],
{
name: '5.0',
y: '40.2',
sliced: true,
selected: true
},
]
}];
Highcharts.getOptions().colors = Highcharts.map(Highcharts.getOptions().colors, function (color) {
return {
radialGradient: {
cx: 0.5,
cy: 0.3,
r: 0.7
},
stops: [
[0, color],
[1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
]
};
});

// Build the chart
$('#container').highcharts({
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Browser market shares. January, 2015 to May, 2015'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
},
connectorColor: 'silver'
}
}
},
series: obj
});


}

最佳答案

整个部分:

foreach ($OSlist as $key => $val) {
// echo "$key = $val" . "<br>";
$category = $category . $key . ",";
$pieData = $pieData . "[" . $key . "," . $val . "],";
}
$pieData="[".$pieData."]";
echo $pieData;
// print_r($OSlist);
} ?>
<script >
$('document').ready(function () {
var jsArray = <?php echo $pieData; ?>;
alert(jsArray);

});


</script>

看起来本质上确实有缺陷。

你应该:

$pieData = json_encode($OSlist);

在脚本标签中,您可以简单地执行以下操作:

 jsArray = JSON.parse("<?php echo $pieData; ?>");

然后在JS中进行HighChart的相关数组格式化。或者你可以直接在 PHP 中先进行格式化,然后对最终形式进行 json 编码,然后直接将其传递给 js,然后再次需要 JSON.parse 它们。

在这些类型的情况下,始终以 JSON 形式进行数据事务。

希望有帮助。

希望有帮助。

关于javascript - 将十进制字符串从 php 传递到 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38284914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com