Reputation: 101
This is a simple function I am using to retrieve text from an XML document. It is working flawlessly in every browser, but it is frequently (not always) failing on Safari on iPad.
When it fails, it returns a 412
status code with the message
Failed to load resource: the server responded with a status of 412 (Precondition Failed).
When I am testing on the iPad, I am on wireless, while my other testing is all done through a wired connection. I tried it on my phone as well, and I did not have any problems. The fact that the error only occurs sometimes makes me wonder if it is related to some kind of race condition or timing issue, but I am at a complete loss here.
function getText(page, ID){
if(languageText == null){
url = directory + "/text/" + language + "/text.xml";
$.ajax({
url: url,
type: "post",
dataType: "xml",
async: false,
success: function(data, status, jqXHR){
console.log(data);
languageText = $(data);
}, error: function(jqXHR, textStatus, errorThrown){
console.log(errorThrown);
console.log(jqXHR);
}
});
}
var selector = "page[id='" + page + "'] text[id='" + ID + "']";
result = languageText.find(selector).text();
return result;
}
Upvotes: 0
Views: 5352
Reputation: 559
Another problem might be that apache replies to a post request with the status code 412 instead of 304 (not modified) if no data has changed (Thanks to http://avnwx.blogspot.co.at/2011/10/debuggers-lie.html for pointing this out!).
To make sure every post request is different I simply add
'?uuid=' + new Date().getTime()
to the url of the ajax request (not the posted data!).
Example request:
jQuery.ajax({
cache: false,
data: {
start: 1,
},
url: 'http://127.0.0.1/test.php?uuid='+new Date().getTime(),
success: function(){ alert('success'); }
}
Upvotes: 1
Reputation: 101
There actually appears to be an issue with how iOS7 handles repeated identical POST requests.
http://www.looseideas.co.uk/ios-7-412-precondition-failed
I changed the POST to GET, and it seems to have fixed the issue.
Upvotes: 4
Reputation: 26
Use an asynchronous connection
async: true
There are several reported problems with synchronous connections and Safari on the iPad https://www.google.com/search?q=syncronous+ajax+ipad+safari.
Upvotes: 1
Reputation: 2636
looks like mod_security problem.
put this in your .htaccess file:
<Files text.xml>
SecFilterInheritance Off
</Files>
Upvotes: 0