1

Possible Duplicate:
How to use Basic Auth and Jquery and Ajax

I have a website were I login via basic authentication, that works fine. But when I want to download a .pdf file (that is generated by the server) I get a popup with the information that I need to login...again :/.

How can I prevent this? Or how can I send the basic authentication information with the link? I saw some examples, like: http://username:password@yourserver.com/filename.pdf . But that don't work in IE.

How can I solve this with only html and or jQuery?

edit: I login via a form in the site, that works. But when I want to download an .pdf file while i'm logged in, I get the pop-up.

<a href="http://yourserver.com/yourpdffile.pdf">Report1.pdf</a>
Community
  • 1
  • 1
Anna Smother
  • 322
  • 2
  • 4
  • 14

2 Answers2

3

Basic authentication is Server based, HTML and jQuery are helpless here.

However, before sending AJAX request to such page, you can provide these details. Here is an example.

$.ajax({
    'url': 'http://yourdomain.com/action/',
    'beforeSend': function(xhr) {
        xhr.setRequestHeader("Authorization", "Basic " + encodeBase64(username + ":" + password) 
    },
    //.....
});
Starx
  • 77,474
  • 47
  • 185
  • 261
  • I'm not sending an ajax request when I retrieve the pdf. It's just a link and I need to authenticate again when I retrieve the pdf. – Anna Smother Nov 12 '12 at 11:08
  • @AnnaSmother, If that link has HTTP authentication, then it will called automatically on navigation. – Starx Nov 13 '12 at 08:43
0

Theoretically, the way you've written is possible (and should be supported by both, browser and server). But however, that's

  1. A bad idea because of security (everyone could see the username and the password!) and
  2. As you have written, it's not fully supported.

I don't know why it doesn't work in IE, but it's better not to use this.

Jawa
  • 2,336
  • 6
  • 34
  • 39
looper
  • 1,929
  • 23
  • 42