發送2次我的AJAX代碼如下:HTTP請求是通過AJAX
data = new FormData();
// data="";
paths = "";
// Create a new HTTP requests, Form data item (data we will send to the server) and an empty string for the file paths.
xhr = new XMLHttpRequest();
// Set how to handle the response text from the server
xhr.onreadystatechange = function(ev){
//console.debug(xhr.responseText);
// console.log("success"+xhr.responseText);
try{
console.log($.parseJSON(xhr.responseText));
var data=$.parseJSON(xhr.responseText);
if(data.success=="yes"){
projectCounter++;
var projectName=$.parseJSON(data.arguments);
console.log(projectName.projectName);
console.log('update table');
if(projectCounter==2){
UploadComplete(projectName.projectName);
}
}
}
catch(e){}
};
// Loop through the file list
for (var i in files){
// Append the current file path to the paths variable (delimited by tripple hash signs - ###)
paths += files[i].webkitRelativePath+"###";
// Append current file to our FormData with the index of i
data.append(i, files[i]);
};
// Append the paths variable to our FormData to be sent to the server
// Currently, As far as I know, HTTP requests do not natively carry the path data
// So we must add it to the request manually.
data.append('paths', paths);
// console.log(paths);
// Open and send HHTP requests to upload.php
xhr.open('POST', "upload.php", true);
console.log(data);
xhr.send(this.data);
我面臨的問題是,它發送HTTP請求2次。我收到2次Http響應。我寫了console.log(「更新表」),它顯示了2次。我很困惑,爲什麼我收到2次Http響應,而不是我只發送1個請求的事實?
是否有可能顯示,此代碼運行的背景下? – melc
melc它發送請求upload.php上傳數據upload.php腳本運行良好。我檢查過它 –