Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Web and WebWorks Development

Reply
New Developer
ascavare
Posts: 4
Registered: 11-16-2009

Blackberry Web App Problems ...

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! :smileyhappy:

Anthony

 

 

Please use plain text.
New Developer
ascavare
Posts: 4
Registered: 11-16-2009

Re: Blackberry Web App Problems ...

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

Please use plain text.
Developer
Posts: 107
Registered: 09-25-2008

Re: Blackberry Web App Problems ...

Doing the authentication via XHR works for me in the 5.0 simulator. Maybe it wasn't supported in 4.6?

Please use plain text.
New Developer
ascavare
Posts: 4
Registered: 11-16-2009

Re: Blackberry Web App Problems ...

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 :smileyhappy:

 

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);
             }
          }
     }
}

 

 

Please use plain text.
BlackBerry Development Advisor
pbhattacherjee
Posts: 33
Registered: 08-07-2009

Re: Blackberry Web App Problems ...

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+.

Please use plain text.
New Developer
ascavare
Posts: 4
Registered: 11-16-2009

Re: Blackberry Web App Problems ...

[ Edited ]

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?

Please use plain text.