2013-09-05 3 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 данные становятся пустыми. Я думаю, что это связано с асинхронными аякс-вызовами. Я пробовал с асинхронным: ложь и тайм-аут, но все еще столкнулся с проблемой.

Есть ли способ справиться с этим?

Заранее спасибо ...

+0

любые мысли PLS? – 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, div, metricid) '? –

Смежные вопросы