11-16-2009 09:26 AM
Hello,
I have been bagning my head against the wall trying to figure out this problem. I need to post some information to a REST endpoint that is protected by some basic authentication. Using the xmlhttprequest object everything works fine except for when I use the username and password parameters with calling the open method ( i.e. xmlhttp.open("POST", someData, false, username, password); ). Though this works on the iPhone, Firefox, and most other browsers Blackberry's browser (testing on a Bold os 4.6) will still throw up a pop-up requesting the credetials be set.
I was hoping someone may know what I am doing wrong. Is there a way to keep that pop-up from showing up? I have also tried setting the bse64 header and even putting the username/password in teh url to no avail.
Any ideas would definitely be appreciated. Thanks! ![]()
Anthony
11-19-2009 10:44 AM
Am I to assume that it is not possible to pass basic authentication parameters through to a REST endpoint without the user being alerted to enter them within the Blackberry browser?
If this is the case, does anyone have any suggestions as to how, if the user enters his/hers credentials within the pop-up, to remove this credentials withour clearing the cache (i.e a logout operation in javascript?).
Thanks,
Anthony
11-19-2009 08:28 PM
Doing the authentication via XHR works for me in the 5.0 simulator. Maybe it wasn't supported in 4.6?
11-20-2009 09:36 AM
Hmm thanks for your reply. It helps a bit to hear it works for someone else as that must mean i am doing something wrong. I did notice (testing on Firefox) that my app is trying to POST twice for some reason. I know it isn't called more than once; but perhaps there is something not being set properly and the browser automatically tries again. The first attempt is always rejected due to lack of credentials but teh second goes through.
Here is the code I am to send the request. Perhaps someone might see something wrong here ![]()
Thanks again,
Anthony
function sendThisRequest(type, url, async, parameters, username, password)
{
var http_request;
var returnText = "not set";
if (!type)
{
type = "GET";
}
if (!async)
{
async = false;
}
if (window.XMLHttpRequest)
{
//Mozilla, Safari, Webkit ...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
//set type accordingly to anticipated content type
http_request.overrideMimeType('text/xml');
}
}
else (window.ActiveXObject)
{
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request)
{
alert('Cannot create XMLHTTP instance');
return false;
}
if (type == "POST")
{
http_request.open('POST', url, false, username, password);
//required headers for post
//http_request.setRequestHeader("Content-type", "text/xml" );
//http_request.setRequestHeader("X-Requested-With" , "XMLHttpRequest");
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
try
{
http_request.send(parameters);
}
catch(e)
{
alert("failed on send")
}
http_request.onreadystatechange = alertContents();
}
else if (type == "GET")
{
http_request.open('GET', url, false);
try
{
http_request.send(null);
}
catch(e)
{
alert("failed on send")
}
http_request.onreadystatechange = alertContents();
}
else
{
alert("not suitable type given");
}
function alertContents()
{
if (http_request.readyState == 4)
{
if (http_request.status == 200)
{
if (requestEvent)
{
requestEvent.fire(null, {
message : http_request.responseText
});
}
}
else
{
alert('There was a problem with the request. ' + http_request.status);
}
}
}
}
11-20-2009 11:10 AM
Hi Anthony,
Authentication via XHR wasn't support on the device until 5.0, which is why your code isn't working. Your application should work on 5.0+.
11-20-2009 11:14 AM - last edited on 11-20-2009 11:15 AM
Ah, that would explain it then. I suppose there are no workarounds then, using Base64 headers or the like. I have been experimenting with the latter (and leaving out the XHR authentication) to no success, but perhaps there is another way?
Or do I just accept there is no way to suppress those login pop-ups (and authenticate) on a Blackberry 4.6 device?