使用 AJAX 更新 Javascript 变量
我正在开发一个 FusionCharts 项目,该项目要求我使用 AJAX 请求更新 javascript 变量。 FusionChart 使用 chart1.setDataXML()
标记内的 xml 填充自身,如下所示。
<div id="barGraph">
Bar Graph
</div>
<script language="JavaScript">
var chart1= new FusionCharts("/charts/Column3D.swf", "barGraph", "600", "400", "0", "1");
chart1.setDataXML("<chart caption='Monthly Unit Sales' xAxisName='Month' yAxisName='Units' showValues='0' decimals='0' formatNumberScale='0'><set label='Jan' value='462'/><set label='Feb' value='857'/><set label='Mar' value='671'/><set label='Apr' value='494'/><set label='May' value='761'/><set label='Jun' value='960'/><set label='Jul' value='629'/><set label='Aug' value='622'/><set label='Sep' value='376'/><set label='Oct' value='494'/><set label='Nov' value='760'/><set label='Dec' value='960'/></chart>");
chart1.render("barGraph");
</script>
正如我所说,我需要使用 AJAX 请求更新该脚本中的 XML。我已经创建了一个更新图表的函数,但我不知道如何使 AJAX 发挥作用...这是我的函数
<script type="text/javascript">
function updateChart(domId){
var response= "<chart></chart>"
var chartObj = getChartFromId("barGraph");
chartObj.setDataXML(response);
}
</script>
有什么方法可以让我的 AJAX 请求更新“响应”变量吗?
I am working on a project with FusionCharts that requires me to update a javascript variable with an AJAX request. The FusionChart populates itself with the xml within the chart1.setDataXML()
tag as seen below.
<div id="barGraph">
Bar Graph
</div>
<script language="JavaScript">
var chart1= new FusionCharts("/charts/Column3D.swf", "barGraph", "600", "400", "0", "1");
chart1.setDataXML("<chart caption='Monthly Unit Sales' xAxisName='Month' yAxisName='Units' showValues='0' decimals='0' formatNumberScale='0'><set label='Jan' value='462'/><set label='Feb' value='857'/><set label='Mar' value='671'/><set label='Apr' value='494'/><set label='May' value='761'/><set label='Jun' value='960'/><set label='Jul' value='629'/><set label='Aug' value='622'/><set label='Sep' value='376'/><set label='Oct' value='494'/><set label='Nov' value='760'/><set label='Dec' value='960'/></chart>");
chart1.render("barGraph");
</script>
As I said, I need to update that XML within that script with an AJAX request. I have created a function that updates the chart, but I can't figure out how to bring AJAX into play... Here's my function
<script type="text/javascript">
function updateChart(domId){
var response= "<chart></chart>"
var chartObj = getChartFromId("barGraph");
chartObj.setDataXML(response);
}
</script>
Is there any way I can make my AJAX request update the 'response' variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西:
something like:
我不确定您是否需要帮助进行 ajax 调用或从控制器响应它,我将解释控制器方面。
在控制器中执行一个操作:
render :update 返回将在页面上运行的 javascript。
page.call 将运行第一个参数中给出的函数,并将其余参数传递给 .call 作为 javascript 函数的参数。
I'm not sure if you need help making the ajax call, or responding to it from the controller, I'm going to explain the controller side.
In the controller make an action:
render :update returns javascript that will get run on the page.
page.call will run the function given in the first argument, and pass the rest of the arguments to .call as arguments to the javascript function.
是的。只需使用 XMLHttpRequest 对象发送请求并将 XMLHttpRequest.response 读入您自己的响应变量即可。
关于如何执行此操作的一个非常好的教程:
http://www.w3schools.com/Ajax/Default.Asp
Yes. Simply send out the request using an XMLHttpRequest Object and read the XMLHttpRequest.response into your own response variable.
A very good tutorial on how to do so:
http://www.w3schools.com/Ajax/Default.Asp