HTTP 401 – what’s an appropriate WWW-Authenticate header value?

When indicating HTTP Basic Authentication we return something like: WWW-Authenticate: Basic realm=”myRealm” Whereas Basic is the scheme and the remainder is very much dependent on that scheme. In this case realm just provides the browser a literal that can be displayed to the user when prompting for the user id and password. You’re obviously not … Read more

What’s the best way to retry an AJAX request on failure using jQuery?

Something like this: $.ajax({ url : ‘someurl’, type : ‘POST’, data : …., tryCount : 0, retryLimit : 3, success : function(json) { //do something }, error : function(xhr, textStatus, errorThrown ) { if (textStatus == ‘timeout’) { this.tryCount++; if (this.tryCount <= this.retryLimit) { //try again $.ajax(this); return; } return; } if (xhr.status == 500) … Read more

Prevent redirection of Xmlhttprequest

Not according to the W3C standard for the XMLHttpRequest object (emphasis added): If the response is an HTTP redirect: If the origin of the URL conveyed by the Location header is same origin with the XMLHttpRequest origin and the redirect does not violate infinite loop precautions, transparently follow the redirect while observing the same-origin request … Read more

Uploading multiple files using formData()

You have to get the files length to append in JS and then send it via AJAX request as below //JavaScript var ins = document.getElementById(‘fileToUpload’).files.length; for (var x = 0; x < ins; x++) { fd.append(“fileToUpload[]”, document.getElementById(‘fileToUpload’).files[x]); } //PHP $count = count($_FILES[‘fileToUpload’][‘name’]); for ($i = 0; $i < $count; $i++) { echo ‘Name: ‘.$_FILES[‘fileToUpload’][‘name’][$i].'<br/>’; }

Add a “hook” to all AJAX requests on a page

NOTE: The accepted answer does not yield the actual response because it is getting called too early. You can do this which will generically intercept any AJAX globally and not screw up any callbacks etc. that maybe have been assigned by any third party AJAX libraries. (function() { var origOpen = XMLHttpRequest.prototype.open; XMLHttpRequest.prototype.open = function() … Read more

AngularJS Error: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https

This error is happening because you are just opening html documents directly from the browser. To fix this you will need to serve your code from a webserver and access it on localhost. If you have Apache setup, use it to serve your files. Some IDE’s have built in web servers, like JetBrains IDE’s, Eclipse… … Read more

Is onload equal to readyState==4 in XMLHttpRequest?

This is almost always true. One significant difference, however, is that the onreadystatechange event handler also gets triggered with readyState==4 in the cases where the onerror handler is usually triggered (typically a network connectivity issue). It gets a status of 0 in this case. I’ve verified this happens on the latest Chrome, Firefox and IE. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)