繁体   English   中英

Ajax请求无法在Safari,Chrome中运行

[英]Ajax request not working in Safari, Chrome

我有一个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中,因此出现错误信息。 我在这里找到了相关信息: 链接

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

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

您的Ajax请求使用POST但不发布任何数据。 您传递的数据是通过您的URL请求参数。

尝试将ajax类型更改为GET

type: "GET",      
url: "/validate/softwareDownloadedCustomer.asp?custID=<%=custID%>&fileName=" + theFileName, 

或通过发布数据传递参数

var dataToPost = 'custID=<%=custID%>&fileName=' + theFileName;

type: "POST",      
url: "/validate/softwareDownloadedCustomer.asp", 
data : dataToPost,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM