1

I want to post username and password to my backend.
Problem is that the data doesn't reach my backend, with following code:

function register() {


  var text = '{"username":"admin1","password":"1234"}';

  var xhr = new XMLHttpRequest();
  xhr.open("POST", "http://localhost:8087/meetmeserver/api/admin/register", true);
  xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
  xhr.send(JSON.stringify(text));

}

If I leave out the line with xhr.setRequestHeader, it reaches my backend but throwing an exception...

Does anyone now where my problem is? I really would appreciate your help. If you need more code just tell me.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Leonard Michalas
  • 1,130
  • 1
  • 11
  • 26

1 Answers1

0

I found a solution by myself. The problem was that both my backend and my frontend were on localhost, but on different ports. There are some security options in your browser which are blocking this.

If you need more information on this look here: "No 'Access-Control-Allow-Origin' header is present on the requested resource"

also here is my improved javaScript... :)

$.ajax({
    url: 'http://localhost:8087/meetmeserver/api/admin/register',
    type: 'POST',
    data: JSON.stringify(json),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    async: false,
    success: function(msg) {
        alert(msg);
    }
});
Community
  • 1
  • 1
Leonard Michalas
  • 1,130
  • 1
  • 11
  • 26