【发布时间】:2012-06-05 23:58:24
【问题描述】:
我有一个 ajax 请求,当单击链接下载文件时会触发该请求。调用服务器端页面,使用访问者下载的客户 ID 和文件名更新数据库。使用 Jquery 1.5.1。这是链接html:
<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 等)? -
谢谢,我无法获取使用 firebug 控制台抛出的类型和异常的值。使用警报时,“类型”的值为“错误”,我得到“错误抛出”的空白值。抱歉含糊不清,我对使用 firebug 调试和获取错误参数的值没有经验。 “request.status”的值为“HTTP错误”,request.statusText为“0错误”。
-
关于响应:softwareDownloadedCustomer.asp 返回简单文本“数据已添加到服务器。”我试图将响应插入到包含 ajax 请求的页面中,如下所示: var response = XMLHttpRequest.responseText; $("#serverResponse").innerHTML=response;但由于我收到一条错误消息,#serverResponse 中没有显示任何内容。然而,在 firefox 和 IE 中,数据被发布到数据库中(我在单独的页面上写入数据插入的记录内容),但数据不会使用 chrome 或 safari 发布到数据库中。
标签: javascript jquery ajax asp-classic