【发布时间】:2017-06-06 11:42:44
【问题描述】:
我正在尝试从 mysql 数据库中提取数据并绘制在浮点图中。我看过几十个帖子,但它对我不起作用。 我成功获得了在 php 中工作的浮点图表 http://oven.luckybat.space/batoven1.0_s.php
首先这里是数据的 url intellipool.overcast.net.au/PCsystem/getdataforflot.php 它似乎是正确的格式。 我想绘制温度与时间的关系
我在加载运行此脚本的页面时收到“错误警报”
有一个
<p> id="graph3" style="width:1000px;height:500px;">graph3</p>
在 wordpress 网页上
感谢帮助
$.ajax({
type: "GET",
dataType: 'json',
url: 'http://intellipool.overcast.net.au/PCsystem/getdataforflot.php',
error: function() {
alert("An error occurred.");
},
success: function(data) {
alert("got the data dude");
var options = {
xaxis: {
mode: "time",
mintickSize: [1, "hours"],
axisLabel: 'Time',
min: ((new Date().getTime()) - 24 * 60 * 60 * 1000),
max: (new Date()).getTime()
},
yaxes: [{
tickFormatter: function(val, axis) {
return val + "\u00B0C";
},
max: 50,
axisLabel: "Temperature",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelPadding: 5
}, {
position: "right",
tickFormatter: function(val, axis) {
return val + "%";
},
max: 100,
axisLabel: "Humidity",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelPadding: 10
}],
};
$.plot("#graph3", data, options);
}
});
按照我添加的说明
<header> ('Access-Control-Allow-Origin: *'); </header>
致http://intellipool.overcast.net.au/PCsystem/getdataforflot.php
我仍然收到错误,我需要在服务器上做更多事情吗(通过联系主机?)
我需要将传入的 JSON 数据分配给变量吗?
完整的函数()是
function ajaxplot() {
document.write('hello!! there ajaxplot() AJAX has been called');
$.ajaxSetup({ cache: false });
$.ajax({
type: "GET",
dataType: 'json',
url: 'http://intellipool.overcast.net.au/PCsystem/getdataforflot.php',
error: function() {
alert("An error occurred.");
},
success: function(data) {
alert("got the data dude");
var options = {
xaxis: {
mode: "time",
mintickSize: [1, "hours"],
axisLabel: 'Time',
min: ((new Date().getTime()) - 24 * 60 * 60 * 1000),
max: (new Date()).getTime()
},
yaxes: [{
tickFormatter: function(val, axis) {
return val + "\u00B0C";
},
max: 50,
axisLabel: "Temperature",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelPadding: 5
}, {
position: "right",
tickFormatter: function(val, axis) {
return val + "%";
},
max: 100,
axisLabel: "Humidity",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelPadding: 10
}],
};
$.plot("#graph3", data, options);
}
});
}
【问题讨论】:
-
它只显示
No 'Access-Control-Allow-Origin' header is present on the requested resource.这个错误意味着你可以找到here -
所以 $ajax url 需要和 js 代码所在的网站是同一个域??
-
是的,这正是问题所在,您是否有权访问您所调用的服务器
http://intellipool.overcast.net.au/PCsystem/getdataforflot.php?如果是,则必须Enable CORS到服务器 -
我已将 $ajax url 文件移至 overcast.net.au/getdataforflot.php,但仍无法正常工作!如果我也移动所有 .js 文件,那会起作用吗?我可以询问网络托管人是否可以更改“启用 CORS 到服务器”
标签: jquery mysql ajax charts flot