XMLHttpRequest in JS not working on Safari

Viewed 578

Searched all over google and here and tried a few things. The page I've setup can only have Javascript code. I've tried the below for a request to pull some details. It works on Chrome and other browsers just not on Safari. I get a "Failed to load resource: the server responded with a status of 404 ()" on Safari v13.1.

Assuming this needs different headers sending for Safari or CORS is causing some issue. Not sure on this. Have tried the fetch method and that's not worked and given the same problem. I'm sure a lot of people have had this issue and found a fix for it in JavaScript. Any help on this would be greatly appreciated.

XML Request JS

var xhr = new XMLHttpRequest();
console.log(xhr);

xhr.onreadystatechange = function () {
    if (xhr.readyState !== 4) return; {
        console.log(JSON.parse(xhr.responseText));
    }
};
xhr.open("GET", "/api/storefront/orders/" + buildOrderID() + "");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.send();

Fetch Request JS

fetch("/api/storefront/orders/" + buildOrderID() + "").then(function (response) {
    // The API call was successful!
    return response.json();
}).then(function (data) {
    // This is the JSON from our response
    console.log(data);
}).catch(function (err) {
    // There was an error
    console.warn('Something went wrong.', err);
});
0 Answers
Related