Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a JSONP call with JQuery with Basic Authentication?

Using JQuery, how would I call a JSONP API that required basic authentication?

Can I programatically handle this, or does the user have to interact and enter the username and password into the logon dialog?

like image 786
Jason Avatar asked Oct 28 '09 21:10

Jason


People also ask

Can JSONP be used with Ajax?

JSONP allows you to sidestep the same-origin policy and to some extent make cross-domain Ajax calls. It's not a silver bullet, and it certainly has its issues, but in some cases it can prove invaluable when fetching data from a different origin.

What is JSONP in Ajax?

JSONP stands for JSON with Padding. Requesting a file from another domain can cause problems, due to cross-domain policy. Requesting an external script from another domain does not have this problem. JSONP uses this advantage, and request files using the script tag instead of the XMLHttpRequest object.

What is dataType JSONP?

dataType: jsonp for cross-domain request, that means request to different domain and dataType: json for same domain-same origin request. Loads in a JSON block using JSONP. Adds an extra "? callback=?" to the end of your URL to specify the callback.


1 Answers

The JSONP request does not use the XMLHttpRequest Object / System. Hence there is no way to do this directly.

I tried several times the approach mentioned in FinnNk's answer, but finally gave up after reading this discussion on the JQuery mailing list.

Update

When I said "no way to do this directly", what I meant is:

  • You can't programmatically set the HTTP auth credentials (However, most browsers I have tested will prompt the user for credentials)
  • If you don't want the user to enter the credentials manually, you need to use a server to proxy your request. See this blog post for an excellent description and example.
like image 58
HRJ Avatar answered Oct 12 '22 01:10

HRJ