【发布时间】:2015-05-24 05:37:16
【问题描述】:
我正在尝试在单击 href 时对我的服务器进行 Ajax 调用。单击我的 href 时,我成功地转到我的服务器并调用定义的路由。我的路线正确地为我服务并将数据发回给我。下面是执行此操作的脚本。
$('.graph-switch').click(function (){
event.preventDefault();
$.getScript("js/ejs_production.js", function()
$.ajax({
url:'/spiderSwitch',
type: 'GET',
data: { type: $(this).attr('href') },
success: function(result){
console.log(result); // correctly displayed
// WHAT TO DO AT THIS POINT??
},
error: function(){
alert("well this is not working");
}
});
});
});
此时我需要重新渲染我想要更改的部分。下面是我要更改的 HTML 部分。
<div class="panel-body text-center"><% include partials/spider-chart.ejs %> </div>
基本上,我使用 ejs 的 include 关键字添加了一个 spider-chart.ejs 文件。下面是 spider-chart.ejs 文件。
<!-- views/partials/spider-chart.ejs -->
<!--NOTE: host pages must include the following in <head>-->
<!--<script src='http://code.jquery.com/jquery-1.6.1.js'></script>-->
<!--<script src="scripts/SpiderChartControl.js"></script>-->
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/highcharts-more.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div id="chart" style="min-width: 300px; max-width: 1000px; height: 300px; margin: 0 auto"></div>
<script>
var spiderGraphData = <%- JSON.stringify(spiderGraphData) %>
var options = {
metric: 'accuracy'
}
console.log(JSON.stringify(spiderGraphData));
SpiderChartControl.draw("chart", spiderGraphData, options);
</script>
基本上,它是一个使用高图表库填充 div 的脚本。据我了解,有两种方法我都未能成功。
类似这样的:
$("#id").innerHtml = "<div class='panel-body text-center'> <% include partials/spider-graph.ejs %> </div>";
或类似使用 ejs 构造的东西:
new EJS({url : 'partials/spider-chart.ejs' }).update('.tab-pane active', result);
我对这个问题没有寄予厚望,但在任何网络资源中都没有对这个特定问题的答案。
【问题讨论】:
-
您在哪一部分遇到了麻烦?我看到您有一个小错误,即 event.preventDefault(),未定义事件,您应该在函数处理程序中定义它,例如 fuction(event) { event.preventDefault() ....
-
我在缺少使用新 EJS 构造执行此操作的文档时遇到了问题。这就是为什么我最终通过更改内部 html 来使其工作。
-
嘿,我也有类似的问题。我使用 ajax 将用户的电子邮件和密码发送到服务器,如果它们是正确的,那么我需要将用户带到另一个页面吗?这怎么可能。我可以使用 ajax 来做到这一点吗
标签: javascript ajax node.js highcharts ejs