javascript - Ajax 请求在 Safari、Chrome 中不起作用

标签 javascript jquery ajax asp-classic

我有一个 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 中运行,我缺少什么?

最佳答案

我实际上找到了导致问题的原因 - 这是由于 ajax 请求被激活以下载文件的链接中断。通过单击链接触发了 ajax 请求。错误消息的请求状态为 0,这意味着链接操作显然中断了 ajax 请求。由于某种原因,显然在 IE 中请求没有被中断,但它在 firefox、safari 和 chrome 中,因此出现错误消息。我在这里找到了相关信息:link .

在测试 ajax 请求触发简单的按钮点击而不是链接时,我知道是这种情况。它在所有浏览器中都能成功运行,当通过简单的按钮点击触发时没有错误消息。

解决方法是不使用 AJAX,而是将用户发送到记录点击的服务器端页面,然后重定向到适当的下载链接。

关于javascript - Ajax 请求在 Safari、Chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10906604/

相关文章:

javascript - 在 Google App 脚本中发送 HEAD 请求

javascript - 如何自动刷新div

javascript - 将参数从 javascript 传递到 Controller

jquery - 动态文本导致网页底部出现空白

javascript - jquery - 根据当前高度与限制切换 div 高度

php - ajax 请求返回意外输出,但输出正确

python - Django刷新页面

javascript - 在 Facebook 上查看 XS Cookie

Javascript颜色矩形数组

php - 表单到 iframe 提交 - 文件上传后如何重置表单?