2013-09-05 80 views
1

我正在開發一個打印頁面,其中我們必須顯示所有的報告圖。多個ajax調用jquery mvc

每個客戶的圖表數量是不同的。 所以,我爲每個可用的圖形寫jQuery腳本如下:

buildJQs: function() { 

     $(".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, 
       async: false, 
       timeout: 300, 
       data: JSON.stringify(m_oReport.printp(name, graphid, metricid)), 
       beforeSend: function() { 
        //Displays loading image before request send, till we get response. 
        //$("#" + divId).addClass("loading"); 
       }, 
       cache: false, 
       success: function (data) { 
        // if they define a success function (s), call it and return data to it. 
        if (typeof m_oReport.prints === "function") { 
         //$("#" + divId).removeClass("loading"); 

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

的問題,數據是沒有任何問題的,從控制器返回,但是當我在分配給jqplot數據正在成爲空。我認爲這是由於異步ajax調用。我嘗試使用async:false和timeout,但仍然面臨問題。

有什麼辦法可以處理嗎?

在此先感謝...

+0

任何想法請? – mmssaann

+0

檢查這是否有幫助http://stackoverflow.com/questions/10877438/jqplot-chart-using-asp-net-mvc-json – ali

回答

1

試試這個用jQuery延遲:

buildJQs: function() { 

    var deferreds = [], deferred; 
    $(".emailgraphs").each(function() { 

     deferred = YAHOO.Report.Print("Email", $(this).attr("responsefield"), $(this).attr("id"), $(this).attr("metricid")); 
     deferreds.push(deferred); 
    }); 

    $.when.apply(null, deferreds).done(function() { 
     alert('all done'); 
    }); 
}, 

Print: function (name, graphid, divid, metricid) { 

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

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

     } 
     catch (err) { alert("catch"); } 
     return null; 
    } 
+0

謝謝你的回答總是得到未定義的延遲行 - 延期=「YAHOO.Report.Print」(「Email」,$(this).attr(「responsefield」),$(this).attr(「id」),$(this).attr(「metricid」)); deferreds.push(延遲); – mmssaann

+0

你使用的是什麼版本的jQuery?爲什麼你用'try' ...'catch'? –

+0

'YAHOO.Report.Print'是'Print:function(name,graphid,divid,metricid)'? –