2014-03-13 73 views
0

對於我的項目,我需要從遠處的服務器獲取一些數據, X次與AJAX。

其實它是在IE瀏覽器工作正常,但是當我嘗試在Chrome和Firefox瀏覽器的AJAX功能不起作用了$(文件)。就緒()事件的...

$(document).ready(function() { 
    doLaunch(); // This works 
}); 

setInterval(function() { 
    if ($_dragStart == null) { 
    doClear(); // working but no AJAX in there 

    doRecup(); // not working 
    } 
}, 10000); 


function doLaunch() { 
    $.ajax({ 
    type: 'POST', 
    url: 'journalier.aspx/copyDataYesterday', 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    complete : function() { 
     doRecup(); // working in there 
    }, 
    error: function (xhr, status, error) { 
     var err = eval("(" + status + ")"); 
     alert(err); 
    } 
    }); 
}; 

function doRecup() { 
    $.ajax({ 
    type: 'POST', 
    url: 'journalier.aspx/getDataNav', 
    contentType: 'application/json; charset=utf-8', 
    data: '{datePlanning:"' + '<%= Session["Planning_Date"].ToString() %>' + '"}', 
    dataType: 'json', 
    success: function (data) { 
     var dateMaj = document.getElementById("MainContent_dateTimePlanning").value; 

     for (i = 0; i < 1000 ; i++) { 
     if (data.d[(i * 5) + 3] == dateMaj) { 
      var objet = document.getElementById(data.d[(i * 5) + 2]); 

      var row = document.getElementById("MainContent_" + data.d[i * 5]); 

      if (row != null) { 

      if (data.d[(i * 5) + 1] == "Ressource") 
       $(objet).appendTo(row.cells[12].childNodes[0]); 
      else if (data.d[(i * 5) + 1] == "Conducteur") 
       $(objet).appendTo(row.cells[4].childNodes[0]); 
      else if (data.d[(i * 5) + 1] == "Chauffeur") 
       $(objet).appendTo(row.cells[6].childNodes[0]); 
      else if (data.d[(i * 5) + 1] == "Engin") 
       $(objet).appendTo(row.cells[5].childNodes[0]); 
      else if (data.d[(i * 5) + 1] == "Chef") 
       $(objet).appendTo(row.cells[11].childNodes[0]); 
      else if (data.d[(i * 5) + 1] == "Consigne" && data.d[(i * 5) + 4] != null) { 
       row.cells[9].childNodes[0].innerText = data.d[(i * 5) + 4]; 
       row.cells[9].childNodes[1].value = data.d[(i * 5) + 4]; 
      } 
      else if (data.d[(i * 5) + 1] == "Carburant" && data.d[(i * 5) + 4] != null) { 
       row.cells[3].childNodes[0].innerText = data.d[(i * 5) + 4]; 
       row.cells[3].childNodes[1].value = data.d[(i * 5) + 4]; 
      } 

      } 

     } 

     } 

     var currDate = new Date(); 

     var HH = currDate.getHours(); 
     var MM = currDate.getMinutes(); 

     var Time = HH + ":" + MM ; 

     document.getElementById("MainContent_lblDateLastMaj").innerText = "Dernière Mise à Jour : " + Time; 

    }, 
    error: function (xhr, status, error) { 
     var err = eval("(" + status + ")"); 
     alert(err); 
    }, 
    complete: function() { 
     alert("Complete!"); 
    } 
    }); 
}; 

function doClear() { 
    for (k = 0; k < document.getElementById("MainContent_divTabProjet").childNodes.length/2; k++) { 
    for (i = 0; i < document.getElementById("MainContent_tabProjetJourAgence" + k).rows.length; i++) { 

    document.getElementById("MainContent_lblCarbu" + i).innerText = ""; 
    document.getElementById("MainContent_lblCarbu" + i).value = ""; 

    for (j = 0; j < document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[4].childNodes[0].childNodes.length ; j++) { 

     $(document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[4].childNodes[0].childNodes[j]).appendTo($("#MainContent_divTabRessources")); 

    } 

    for (j = 0; j < document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[5].childNodes[0].childNodes.length ; j++) { 

     $(document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[5].childNodes[0].childNodes[j]).appendTo($("#MainContent_divTabEngins")); 

    } 

    for (j = 0; j < document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[6].childNodes[0].childNodes.length ; j++) { 

     $(document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[6].childNodes[0].childNodes[j]).appendTo($("#MainContent_divTabRessources")); 

    } 

    document.getElementById("MainContent_lblConsigne" + i).innerText = ""; 
    document.getElementById("MainContent_lblConsigne" + i).value = ""; 

    for (j = 0; j < document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[11].childNodes[0].childNodes.length ; j++) { 

     $(document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[11].childNodes[0].childNodes[j]).appendTo($("#MainContent_divTabRessources")); 

    } 

    for (j = 0; j < document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[12].childNodes[0].childNodes.length ; j++) { 

     $(document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[12].childNodes[0].childNodes[j]).appendTo($("#MainContent_divTabRessources")); 

    } 
    } 
} 
}; 

更新:當我在控制檯中手動啓動功能時,它工作正常。所以問題在於函數不是在間隔中啓動,而是doClear函數每10秒啓動一次......如果我將doClear函數置於註釋中,doRecup函數每隔10秒觸發一次。

更新2:如果我這樣做,它的工作原理

setInterval(function() { 
if ($_dragStart == null) { 
    doClear(); 
} 
}, 10000); 
setInterval(function() { 
if ($_dragStart == null) { 
    doRecup(); 
} 
}, 10000); 

更新3:所以現在我那些:

setInterval(function() { 
    setTimeout(function() { 
    doClear(); 
    }, 1); 
    setTimeout(function() { 
    doRecup(); 
    }, 1); 
}, 10000); 

那麼它遠不是完美的,它肯定不是推薦,但實際上它的工作原理如此...

+0

什麼不工作?你在控制檯中是否有任何錯誤? – anderssonola

+0

@anderssonola我沒有錯誤,但doRecup函數只在ready事件中啓動,而不是每10秒啓動一次。 doClear函數正常工作。 –

回答

0

我會建議您調用基於該函數是否ajax請求完成。通過在setInterval中調用該函數,無論請求是否完成,ajax調用都會每隔x秒進行一次,在我看來這不是一個好習慣。我會做這樣的事情:從

function doRecup() { 
....... 
....... 
....... 
....... 
complete: function() { 
    setTimeout(function(){ 
    doRecup(); 
    doClear(); 
    }, 10000) 
} 
....... 
....... 
....... 
} 

和註釋/刪除功能:

setInterval(function() { 
    if ($_dragStart == null) { 
    //doClear(); // working but no AJAX in there 

    //doRecup(); // not working 

    } 
}, 10000); 
+0

用這種方法,我可以確定doRele()會在doClear()之後觸發嗎? –

+0

查看編輯。你試圖看看它是否有效?用jQuery重新編寫doClear()函數會很好,因爲您仍然可以使用jQuery,這會更易讀易懂。 – lshettyl

+0

它沒有工作,問題仍然是一樣的,doClear()是觸發器,但不是doRecup(),因爲doRelecle()必須放在doClear()之後,因爲當成功時表需要爲空事件是觸發器。 –

0

你要爲var $_dragStart = null任何地方..?因爲,對我來說你的代碼工作。

無論如何,你可以使用custom ajax error method知道那裏發生了什麼..因爲你的代碼'var err = eval("(" + status + ")");'不工作,並給出錯誤。

我與你分享我的功能

Custom Ajax Error Handling Code:

/** 
* Custom Ajax Error Method 
* 
* @param jqXHR 
* @param exception 
* @returns {string} 
*/ 
function ajaxError(jqXHR, exception) { 
    if (jqXHR.status === 0) { 
     alert('Not connected.\nPlease verify your network connection.'); 
    } else if (jqXHR.status === 404) { 
     alert('The requested page not found. [404]'); 
    } else if (jqXHR.status === 500) { 
     alert('Internal Server Error [500].'); 
    } else if (exception === 'parsererror') { 
     alert('Requested JSON parse failed.'); 
    } else if (exception === 'timeout') { 
     alert('Time out error.'); 
    } else if (exception === 'abort') { 
     alert('Ajax request aborted.'); 
    } else { 
     alert('Uncaught Error.\n' + jqXHR.responseText); 
    } 
} 

,你可以使用這樣的:

Example Code:

function doLaunch() { 
    // ajax call 
    $.ajax({ 
     type : 'POST', 
     url : 'ajaxCall.php', 
     contentType : 'application/json; charset=utf-8', 
     dataType : 'json', 
     complete : function() { 
      console.log('Calling: Ajax Complete doLaunch()'); 
      doRecup(); // working in there 
     }, 
     error : ajaxError 
    }); 
    // end ajax call 
}; 

你的代碼在小提琴中工作。只是檢查它..

小提琴DEMO

+0

我認爲我的問題不是來自AJAX,它更像是從我的doClear()函數中進行的... –

相關問題