我有一個ajax請求,當單擊鏈接下載文件時會觸發。調用一個服務器端頁面,用訪問者下載的客戶ID和文件名更新數據庫。使用Jquery 1.5.1。這裏的鏈接HTML:Ajax請求無法在Safari,Chrome中運行
<a href="/downloads/whatever.zip" class="software">click here to download</a>
Ajax請求被寫成如下,幷包含一個函數來通知,如果有在Ajax請求的錯誤:
$(document).ready(function() {
//function to alert an error message if request is unsuccessful
function ajaxError(request, type, errorThrown)
{
var message = "There was an error with the AJAX request.\n";
switch (type) {
case 'timeout':
message += "The request timed out.";
break;
case 'notmodified':
message += "The request was not modified but was not retrieved from the cache.";
break;
case 'parseerror':
message += "XML/Json format is bad.";
break;
default:
message += "HTTP Error (" + request.status + " " + request.statusText + ").";
}
message += "\n";
alert(message);
}
$('a.software').click(function() {
//get the path of the link, and just the file name to be stored in the database
var filePath = $(this).attr("href");
var partsArray = filePath.split('/');
var theFileName = partsArray[partsArray.length-1];
$.ajax({
type: "POST",
url: "/validate/softwareDownloadedCustomer.asp?custID=<%=custID%>&fileName=" + theFileName,
success: function(msg) {
alert("Success");
},
error: ajaxError
});
});
});
服務器端代碼沒有問題,只要通過另一個頁面調用而不是ajax請求,它就可以按預期工作。奇怪的是,AJAX請求適用於Firefox和IE,但不適用於Chrome和Safari。在IE中,「成功」消息被警告並且數據庫被更新。在Firefox,Chrome和Safari瀏覽器,這是我的警覺錯誤信息如下:
There was an error with the AJAX request.
HTTP Error (0 error)
即使在Firefox收到錯誤消息,它更新數據庫。在Chrome和Safari中,收到錯誤消息並且沒有更新。
,我用我的Ajax調用以下設置嘗試,它似乎並沒有做任何瀏覽器的區別:
contentType: "application/json; charset=utf-8",
dataType: "json", 'data'
我缺少什麼來得到這個工作在Safari和Chrome?
如果你在'ajaxError'中放置一個斷點並且使用控制檯進行調試,'type'和'exceptionThrown'的值是什麼?另外,softwareDownloadedCustomer.asp返回什麼(HTML,JSON等)? – HackedByChinese
謝謝,我無法獲得使用螢火蟲控制檯拋出的類型和異常值。當使用alert時,'type'的值是'error',我爲'errorThrown'得到一個空白值。對不起,如此模糊,我沒有經驗使用螢火蟲進行調試並獲取錯誤參數的值。 'request.status'的值是'HTTP錯誤',request.statusText是'0錯誤'。 – deeholzman
有關響應:softwareDownloadedCustomer.asp返回簡單文本「數據已添加到服務器。「我已經嘗試插入響應到含有Ajax請求如下頁面: VAR響應= XMLHttpRequest.responseText; $(」#serverResponse「)的innerHTML =響應; 但因爲我得到的錯誤消息在#serverResponse中沒有任何顯示,但是在firefox和IE中,數據在數據庫中發佈(我寫入了在單獨頁面上插入數據的記錄內容),但數據不會在數據庫中使用chrome或safari發佈。 – deeholzman