0

This is my code for XMLHttpRequest. I am using XMLHttpRequest to access api.

How to resolve cross browser origin issue?

var url = "http://192.168.1.10:8080/analytic/log/visit/create";
var json = JSON.stringify(data);
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
// xhr.setRequestHeader('method', 'post');
xhr.onload = function() {
    var users = JSON.parse(xhr.responseText);
    if (xhr.readyState == 4 && xhr.status == "201") {
        console.table(users);
    } else {
        console.error(users);
    }
}
xhr.send(json);
Cœur
  • 32,421
  • 21
  • 173
  • 232
nikhil
  • 9
  • 2
  • 1
    Possible duplicate of [AJAX in Chrome sending OPTIONS instead of GET/POST/PUT/DELETE?](https://stackoverflow.com/questions/21783079/ajax-in-chrome-sending-options-instead-of-get-post-put-delete) – Muhammad Usman Jul 10 '18 at 05:48
  • 1
    Possible duplicate of [Why is an OPTIONS request sent and can I disable it?](https://stackoverflow.com/questions/29954037/why-is-an-options-request-sent-and-can-i-disable-it) – Narendra Jadhav Jul 10 '18 at 05:48
  • method='OPTIONS' is a pre-flight request which is logged in browser to check the cross browser issues. – Nikhil Koul Jul 10 '18 at 05:49

1 Answers1

0

Not a valid fix but you can use browser specific plugins like ,

  • Allow-Control-Allow-Origin: *1.0.3 (for chrome, an extension)
  • CORS Everywhere (for firefox,an extension)

its a quick fix but not permanent

serious_luffy
  • 409
  • 1
  • 6
  • 17