achilles
achilles

Reputation: 119

HTTP Basic Authorization on IP Camera using AJAX / jQuery

I understand that this question was asked plenty of times before but it seems that none of the responses worked for me so here's my issue:

I have a Sony IP camera that is in the intranet. I am building a site with PHP/MySQL authentication for internal users to be able to view the MJPEG stream but the camera itself requires http authentication. I do not want to have the users enter their username and password to log into the camera page and then have to type the http authentication credentials (in the popup) to see the stream. I tried using jQUERY to change the headers to no avail. Keep in mind that the camera MUST have its own authentication so that users cannot just randomly type the IP and see the stream. I want to be able to control who views what and when.

I am assuming that if I make a correct authentication call when the user logs into the page, that camera will be available to them since they would have "silently" logged in. Also, if I use wget from the terminal with the --headers: "Authorization: blah_Blah" it actually works but I can't do this from jQuery! Here's my code:

$.ajax({
    url : "http://some_ip_internally_for_the_cam/some_page_on_cam_that_needs_authentication_to_access_otherwise",
    method : 'GET',
    beforeSend : function(req) {
        req.setRequestHeader('Authorization', "some_base_64_stuff_that_works_in_wget");
    },
    success: function() {
       $("div.log").attr("innerHTML", "ok");
    }
});

This gets loaded as soon as the user logs in.

Any suggestions?

Upvotes: 4

Views: 3967

Answers (3)

hanamj
hanamj

Reputation: 132

Have you tried the username and password fields in .ajax?

$.ajax({
    url: "http://mydomain/mypage.php",
    username: "myUsername",
    password: "myPassword",
    error: function () {
    }
}).done(function(html) {
        //Do Stuff
});

This works for me in a similar scenario.

Upvotes: 0

eightyfive
eightyfive

Reputation: 4721

I had the same Javascript error in Firefox:

... 0x804b000f (NS_ERROR_IN_PROGRESS) [nsIXMLHttpRequest.setRequestHeader] ...

As I was trying to set a new HTTP header in some XMLHttpRequest object:

xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");

The error occured because I was trying to set the HTTP header before the XMLHttpRequest.open() method.

Source: http://www.webmasterworld.com/forum91/4542.htm

Upvotes: 2

Vasiliy Faronov
Vasiliy Faronov

Reputation: 12310

If you mean that div.log doesn’t display the “ok” message, it’s probably because your DOM code is incorrect. This:

$("div.log").attr("innerHTML", "ok");

sets a DOM attribute of the div, not its actual innerHTML. Instead, you should do:

$("div.log").html("ok");

Upvotes: 0

Related Questions