简体   繁体   English

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

[英]Ajax request not working in Safari, Chrome

I have an ajax request that fires when a link is clicked to download a file. 我有一个ajax请求,当单击链接下载文件时会触发该请求。 A server-side page is called that updates a database with the customer ID and file name that the visitor has downloaded. 调用服务器端页面,使用访问者下载的客户ID和文件名更新数据库。 Using Jquery 1.5.1. 使用Jquery 1.5.1。 Here's the link html: 这是链接html:

<a href="/downloads/whatever.zip" class="software">click here to download</a>

The ajax request is written as follows, and contains a function to notify if there's an error in the ajax request: 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
    });
  }); 
});

The server side code is not the problem, it works as expected when called simply by another page and not an ajax request. 服务器端代码不是问题,它只是由另一个页面而不是ajax请求调用时按预期工作。 Strangely, the ajax request works in Firefox and IE, but not in Chrome and Safari. 奇怪的是,ajax请求适用于Firefox和IE,但不适用于Chrome和Safari。 In IE, a 'success' message is alerted and the database is updated. 在IE中,警告“成功”消息并更新数据库。 In Firefox, Chrome and Safari, my error message that is alerted is as follows: 在Firefox,Chrome和Safari中,我收到的错误消息如下:

There was an error with the AJAX request.
HTTP Error (0 error)

Even though the error message is received in firefox, it updates the database. 即使在firefox中收到错误消息,它也会更新数据库。 In chrome and safari, the error message is received and nothing is updated. 在chrome和safari中,会收到错误消息,并且不会更新任何内容。

I've tried using the following settings in my ajax call, and it didn't seem to make a difference in any of the browsers: 我尝试在我的ajax调用中使用以下设置,它似乎没有在任何浏览器中产生影响:

contentType: "application/json; charset=utf-8",
dataType: "json", 'data' 

What am I missing to get this to work in Safari and Chrome? 我在Safari和Chrome中使用了什么?

I actually found what caused the problem - it was due to the ajax request being interrupted by the link being activated to download the file. 我实际上发现了导致问题的原因 - 这是由于ajax请求被链接被激活以下载文件而中断。 The ajax request was fired by clicking the link. 单击链接触发了ajax请求。 The error message's request status was 0, meaning the link action was apparently interrupting the ajax request. 错误消息的请求状态为0,表示链接操作显然正在中断ajax请求。 For some reason apparently in IE the request was not interrupted, but it was in firefox, safari and chrome, hence the error message. 出于某些原因显然在IE中请求没有被中断,但它是在firefox,safari和chrome中,因此出现错误信息。 I found the relevant info here: link . 我在这里找到了相关信息: 链接

I knew this to be the case after testing the ajax request to fire on a simple button click instead of the link. 我知道这是在测试ajax请求以点击一个简单的按钮而不是链接后触发的情况。 It worked successfully in all browsers with no error messages when fired by a simple button click. 它通过简单的按钮点击触发,在所有浏览器中都成功运行,没有错误消息。

The workaround is instead of using AJAX, sending the user to a server-side page that records the click and then redirects to the appropriate download link. 解决方法是使用AJAX,将用户发送到记录单击的服务器端页面,然后重定向到相应的下载链接。

Your Ajax request uses POST but does not post any data. 您的Ajax请求使用POST但不发布任何数据。 The data you are passing is through your URL request parameters. 您传递的数据是通过您的URL请求参数。

Try either changing your ajax type to GET 尝试将ajax类型更改为GET

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

or passing your parameters via post data 或通过发布数据传递参数

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