0

My problem is that I can not send the correct request to the server to get an answer from him in the form of a json line.
Here is my code:

$.ajax({
    type: "GET",
    url: url,
    dataType: 'json',
    crossDomain: true,
    username: 'username',
    password: 'password',
    headers: {
        'Access-Control-Allow-Origin': '*'
    },
    success: function(response) {
        console.log(response);
    }
})

When I try to send a request, I get an error:

XMLHttpRequest cannot load URL. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

Bond - Java Bond
  • 3,972
  • 6
  • 36
  • 59
  • Hi, Please see http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin – MrGoofus Apr 13 '17 at 06:47
  • first you forget the data option in your ajax call. second if there is a symatic failure we need to see what the server request should be like, 3rd. allow origin is one of the most searched failure in combination with ajax i think ;) with thousand off google results – mtizziani Apr 13 '17 at 06:51
  • If the front-end app is on a different domain than the one you are requesting from then you should discuss with the guys from back-end how the data should be send cross-domain. If is about CORS then everything is handled by the browser. Remove the "Access-Control-Allow-Origin" header which is sent by the server. Add "Accept: application/json" to notify the server that data needs to be serialized as JSON. You should reserve some time and read about CORS. It will serve you on the long run. Check this [LINK](http://restlet.com/company/blog/2015/12/15/understanding-and-using-cors/) for example. – andreim Apr 13 '17 at 06:58

1 Answers1

0
var dataSet = {
  username: 'username',
  password: 'password',
}

$.ajax({
  type: "GET",
  url: url,
  dataType: 'json',
  crossDomain: true,
  data: dataSet,
  // the origin header has to be set server side
  /* 
  headers: {
   'Access-Control-Allow-Origin': '*'
  },
  */
  success: function(response) {
    console.log(response);
  }
});

parameters have assigned by a dataSet and the option data. the allow origin header has to be set server side, not in the client

mtizziani
  • 956
  • 10
  • 23