I have an ajax request that fires when a link is clicked to download a file. A server-side page is called that updates a database with the customer ID and file name that the visitor has downloaded. Using Jquery 1.5.1. Here's the link 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:
$(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. Strangely, the ajax request works in Firefox and IE, but not in Chrome and Safari. In IE, a 'success' message is alerted and the database is updated. In Firefox, Chrome and Safari, my error message that is alerted is as follows:
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. In chrome and safari, the error message is received and nothing is updated.
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:
contentType: "application/json; charset=utf-8",
dataType: "json", 'data'
What am I missing to get this to work in Safari and Chrome?
I have an ajax request that fires when a link is clicked to download a file. A server-side page is called that updates a database with the customer ID and file name that the visitor has downloaded. Using Jquery 1.5.1. Here's the link 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:
$(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. Strangely, the ajax request works in Firefox and IE, but not in Chrome and Safari. In IE, a 'success' message is alerted and the database is updated. In Firefox, Chrome and Safari, my error message that is alerted is as follows:
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. In chrome and safari, the error message is received and nothing is updated.
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:
contentType: "application/json; charset=utf-8",
dataType: "json", 'data'
What am I missing to get this to work in Safari and Chrome?
Share Improve this question edited Jun 6, 2012 at 12:14 AnthonyWJones 189k35 gold badges235 silver badges307 bronze badges asked Jun 5, 2012 at 23:58 deeholzmandeeholzman 1891 gold badge4 silver badges15 bronze badges 3-
1
If you put a breakpoint inside
ajaxError
and debug using the console, what are the values oftype
andexceptionThrown
? Also, what does softwareDownloadedCustomer.asp return (HTML, JSON, etc.)? – moribvndvs Commented Jun 6, 2012 at 0:30 - Thanks, I was not able to get value of type and exception thrown using firebug console. When using alert, the value of 'type' is 'error' and I get a blank value for 'errorThrown'. Sorry to be so vague, I am inexperienced at using firebug to debug and get values of error parameters. The value of 'request.status' is 'HTTP error' and request.statusText is '0 error'. – deeholzman Commented Jun 6, 2012 at 15:55
- regarding the response: softwareDownloadedCustomer.asp returns simple text "The data was added to the server." I've attempted to insert the response into the page containing the ajax request as follows: var response = XMLHttpRequest.responseText; $("#serverResponse").innerHTML=response; but since I'm getting an error message nothing displays in #serverResponse. In firefox and IE, however, data is posted in the database (I write contents of the record where data is inserted on a seperate page), but data is not posted in database using chrome or safari. – deeholzman Commented Jun 6, 2012 at 17:08
2 Answers
Reset to default 3I actually found what caused the problem - it was due to the ajax request being interrupted by the link being activated to download the file. The ajax request was fired by clicking the link. The error message’s request status was 0, meaning the link action was apparently interrupting the ajax request. For some reason apparently in IE the request was not interrupted, but it was in firefox, safari and chrome, hence the error message. 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. 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.
Your Ajax request uses POST but does not post any data. The data you are passing is through your URL request parameters.
Try either changing your ajax type to 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,