Dimitris Papazacharias Dimitris Papazacharias - 21 days ago 5
AngularJS Question

xhr object does not have "withCredentials" property in Firefox and Chrome

I want to do CORS requests from a client page. As I'm new to web developing, although I'm using angular for a specific app development, I started researching the issue and I thought I should do it in plain javascript first. In enable-cors.org there is a table which says that Firefox (v 46. and newer) and Chrome (v 51. and newer) both support CORS.
In html5rocks.com they say that you can check if a browser supports CORS by checking that a created XMLHttpRequest object has a "withCredentials" property (let's leave IE out of it for now). So I wrote a very simple function that creates an xhr object and checks for the "withCredentials" property and loads the result on the console, and I call the function via an html button, like this:



'use strict';

var corsPostRequest = function(){

console.log("test");

var xhr = new XMLHttpRequest();
var lookInside = xhr.hasOwnProperty("withCredentials");
console.log(lookInside);

if (xhr.hasOwnProperty("withCredentials")) {
console.log("we \'re good to go..");
}
};// end of corsPostRequest

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="javascripts/corsTest.js"></script>
</head>
<body>
<button id="submit_entry" onclick="corsPostRequest()">POST</button>
</body>
</html>





The result I'm getting in the console is:

test

false

which means that the xhr object does not have the "withCredentials" property and consequently cors isn't possible (?). I have tried this in both Firefox (v 50.0) and Chrome (v 54.0.2840.71) but I'm getting the same result, although both browsers should do otherwise, at least according to the link I mentioned above. So I want to ask:

1) Can this be a problem of windows 10 browsers (on which I'm operating)?
2) Is there any way to do cors under these circumstanses?
3) Does angularjs have a way to work around the problem?

Thanks in advance.

Answer

xhr.hasOwnProperty("withCredentials")

Your mistake here is that you are assuming it is an own property and not a getter/setter pair inherited from a prototype.

"withCredentials" in xhr // true