0

我必须生成几个图形,每个图形数据都应该从 mvc 控制器中获取。

所以我使用下面的 jquery 来做从 jquery 到 mvc 的 ajax 调用

 $(".emailgraphs").each(function () {
 YAHOO.Report.Print("Email", $(this).attr("responsefield"), $(this).attr("id"),     $(this).attr("metricid"));

}); 

 Print: function (name, graphid, divid, metricid) {
 try {
 $.ajax({
 type: "POST",
 contentType: "application/json; charset=utf-8",
 url: m_oReport.ds,
 data: JSON.stringify(m_oReport.printp(name, graphid, metricid)),
 beforeSend: function () {
 //Displays loading image before request send, till we get response.
 //$("#" + divId).addClass("loading");
 },
 success: function (data) {
 // if they define a success function (s), call it and return data to it.
 if (typeof m_oReport.prints === "function") {

 m_oReport.prints(data, divid, name)
 }
 },
 error: function (err) {
 $("#" + divid).html(err);
 }
 });
 }
 catch (err) { alert("catch"); }
 } 

问题是调用是异步的。有时我会得到一个图表,有时会得到 2 个这样的图表,有时什么也没有。

有什么解决办法吗?

4

1 回答 1

2

尝试使用这样的东西

function getDataSync() {
    return $.ajax({
        type: "POST",
        url: remote_url,
        async: false,
    }).responseText;
}
于 2013-09-06T05:26:51.220 回答