2

The documentation of the Geoserver mentions about REST examples. I managed to achieve that using PHP. But I fail with Javascript. It show Status Code 403, which means Forbidden.

enter image description here

Here's my javascript pure code:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       // Action to be performed when the document is read;
    }
};
xhttp.open("GET", urlGeo, true);
xhttp.setRequestHeader('Authorization', "Basic YWRtaW46Z2Vvc2VydmVy");
xhttp.send();

Here's my jquery ajax code:

$.ajax({
url: urlGeo,
type: 'POST',
beforeSend: function (xhr) {
    xhr.setRequestHeader('Authorization', "Basic YWRtaW46Z2Vvc2VydmVy");
},
success: function (res) { console.log('res',res); },
error: function (err) { 
console.log('err',err);
}
});

Both fail miserably. The thing is, no secret about the username and password. I am using the default for testing:

username: admin
password: geoserver

In this case, basic authorization is applied. Basic YWRtaW46Z2Vvc2VydmVy is being converted from btoa(username:password).

So, I wonder why it fails in Javascript?

zeroflaw
  • 388
  • 8
  • 19
  • @Quentin Yes it seems you have pointed out that "GET" becomes "OPTIONS" is the root cause. But I also hope that you can tell why it happens in javascript, but not PHP? In PHP, it is using CURL. I thought it is equivalent to HTTP request in javascript. – zeroflaw Apr 06 '17 at 01:20
  • 1
    http://stackoverflow.com/a/35553666/19068 – Quentin Apr 06 '17 at 08:21

0 Answers0