3

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?

10
  • You get that "random" 401 with Firefox or Chrome or do you get it with Safari and IE? Is there a pattern to it? Like it is the url would be missing the appropriate protocol. You could use console.log rather than alert to get the contents of the JSON variable. Commented Dec 23, 2016 at 7:59
  • For Safari and IE, it is not even get executed and nothing show up in console. now i found that IE do not support user name and password in URL support.microsoft.com/en-in/kb/834489 , so i am looking for any other way do have ajax call which support cross origin with header authentication Commented Dec 23, 2016 at 9:05
  • what version of the ie are you using? Commented Dec 26, 2016 at 8:28
  • I tried with all IE version IE9,IE10, EDGE Commented Dec 26, 2016 at 9:39
  • makes sure to use HTTPS for these type of interactions Commented Dec 30, 2016 at 21:26

4 Answers 4

2
+50

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Kevin, Not sure if I have miss something when I tried with like this before. Its working fine . Thanks
1

For Internet Explorer 8 and 9 you need to use XDomainRequest Object

Internet Explorer 10+ does the cross domain requests normally like all the other browsers.

As mentioned in the documentation you need to

  • create an object of the XDR using var xdr = new XDomainRequest();
  • open the connection using the get method using xdr.open("get", "username:[email protected]");
  • send the data back to the server using xdr.send();

The complete code reference can be shown as on this thread by @Mark Pieszak

as a workaround to set the username and the password in the internet explorer you can set the following

DWORD for iexplore.exe to 0 in: [HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE].

Comments

0

I will recommend you to try two things:

In ajaxSetup, do this:

$.ajaxSetup({
    ....,
    xhrFields: {
       withCredentials: true
    },
    crossDomain: true,
    ....
});

In your ajax requests, set the full url like so, in addition to the credentials flag.

'Access-Control-Allow-Origin: https://not-example.com'
'Access-Control-Allow-Credentials: true'

For servers with authentication, these browsers do not allow "*" in this header. The Access-Control-Allow-Origin header must contain the value of the Origin header passed by the client.

Comments

-3

use getJSON

$.getJSON("url",function (data) {/*code here*/});

1 Comment

$.getJSON() is a shorthand for the $.ajax() jQuery method, were it to solve the issue an explanation would still be required as to why it solves it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.