0

Jquery ajax requests works fine in IE but it gives a below error in chrome

 XHR failed loading: OPTIONS http://someurl/ send @ jquery.min.js:2 ajax @ jquery.min.js:2 (anonymous) @ index.html:6 

event after adding below parameters header

 $.ajax({ beforeSend: function (xhr) { xhr.setRequestHeader ("Authorization", "Basic " + btoa("user" + ":" + "passoword")); }, url: 'url', type: 'get', contentType:'text/html', headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'OPTIONS, HEAD, GET, POST, PUT, DELETE' }, success: function( data, textStatus, jQxhr ){ alert(data); $('#idc').html( data ); }, error: function( xhr, textStatus, errorThrown ){ alert(errorThrown+xhr.status+textStatus); } }); 

Application uses basic authentication and it works fine in ie but in chrome it is not picking the credentials and redirecting to other url where credentials needs to be entered. I gone through all the urls in stackoverflow but nothing worked

12
  • Could you provide some sample code (javascript) please? Also: CORS headers like Acces Control Allow Origin and Access Control Allow Methods are meant to be implemented serverside, and not to be included in the Request headers of your AJAX. Commented Mar 28, 2018 at 12:26
  • 1
    Did you add the 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT', in the backend on the server side? Commented Mar 28, 2018 at 12:27
  • 1
    Add the code used to that ajax request, without it, and only with the respond, it's difficult to guess what's going on. Commented Mar 28, 2018 at 12:27
  • 2
    headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'OPTIONS, HEAD, GET, POST, PUT, DELETE' }, putting this in your request makes no sense. Only the server can set these options. They belong in the response, not the request. Check the MDN documentation for these headers, it mentions they are server-only headers. Only the server gets to decide what methods it accepts, and what origins it allows CORS for. You, as the caller, can't decide that - or else what would be the purpose of CORS at all? Commented Mar 28, 2018 at 12:32
  • 1
    Start by removing headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'OPTIONS, HEAD, GET, POST, PUT, DELETE' }, which will create preflights that are not working with AUTH Commented Mar 28, 2018 at 12:39

2 Answers 2

1

Try this :)

$.ajax({ xhrFields: { withCredentials: true }, headers: { Authorization: "Basic " + btoa("username:password") }, url: "url", type: 'GET', success: function(data, textStatus, jQxhr) { alert(data); }, error: function(xhr, textStatus, errorThrown) { alert(errorThrown + xhr.status + textStatus); } }); 
Sign up to request clarification or add additional context in comments.

Comments

0

you can add some code in htaccess

<IfModule mod_headers.c> Header set Access-Control-Allow-Origin "*" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.