Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross domain request with header authentication

I need to send a Get request with cross domain origin with header authentication.

Its working fine in Chrome and Firefox, but I'm having issues in Safari and IE. Also in random cases it returns 401.

<script>
var url = 'username:[email protected]';
$.ajax({
    url: url,
    dataType: 'jsonp',
    jsonpCallback: "callback",
    success: function(json) {
        alert(json);
    }
});
</script>

What would be the best option to solve this?

like image 343
magento2new Avatar asked Jun 13 '26 17:06

magento2new


1 Answers

If I have understood the question correctly, you could use the beforeSend callback to add basic authentication on the request. This is irrelevant of jsonp or cross-origin though.

beforeSend: function (xhr) {
  xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
}

https://jsfiddle.net/rn9Lp304/

like image 127
Kevin Farrugia Avatar answered Jun 16 '26 08:06

Kevin Farrugia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!