Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Contributor
Posts: 6
Registered: ‎01-25-2011
My Device: Not Specified
My Carrier: Blackberry Application Developer

How to call .NET web-service from javascript in Blackberry WebWorks Application

Hi Experts,

                         I am developing a sample blackberry webworks application. It consists of login page. Here, i need to call .net webservice to authenticate the user. For this I've made use of xmlHttpRequest object.

I'm using following software :-

1.] blackberry JDE 5.0

2.] blackberry smartphone simulator (9550)

 

Problem is i get response from webservice as "null".

 

------------------------------------------------ login.html -----------------------------------------------------

 

 

<html>
<head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <meta name="viewport" id="viewport" content="height=device-height,width=device-width,user-scalable=no" />

</head>

<body onLoad="webservice()">

            <script type="text/javascript">
                 
                 var xmlhttp;
                
                function webservice()
                {
                    alert("----- in webservice -----");
                    
                    if (window.XMLHttpRequest)
                      {
                        // code for IE7+, Firefox, Chrome, Opera, Safari
                            xmlhttp=new XMLHttpRequest();
                      }
                    else
                    {
                        // code for IE6, IE5
                         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                    }
              
                                          
                     xmlhttp.open("POST", "http://192.168.1.179/ms3service/DataService.asmx?op=Authenticate&username=obuser&password=obuser", true);
                                                             
                                
                       if (xmlhttp.overrideMimeType)
                           xmlhttp.overrideMimeType('text/xml');
                           
                       xmlhttp.onreadystatechange = callbackFunction;
                      xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                                          
                        xmlhttp.send();
                                          
                }
                
                
                function callbackFunction()
                {
                 alert("in callbackfunction");
                
                        if (xmlhttp.readyState != 4)
                                  return;
            
                     try
                     {
                        
                        var result = xmlhttp.responseXML;
                        alert(" result == "+result);     // here i get "null"
                                             
                     
                     }
                     catch(e)
                     {
                         alert("err === "+e.description);
                     }
                }
               
            </script>
</body>
</html>

 

-------------------------------------------------------------------------------------------------------------

 

Also, i've doubt regarding whether really the webservice is getting accessed or not. I really can't figure it out.

 

Other issue is regarding contents of config.xml file. Can you guide me for what exactly must be written in  <access uri=""  subdomains="">. This is very important tag.  I guess i've  made a big mistake regarding this tag, & so nothing is working out.

 

-------------------------------------------------- config.xml -------------------------------------------

 

<?xml version="1.0" encoding="UTF-8"?>
<widget xmlns="http://www.w3.org/ns/widgets" xmlns:rim="http://www.blackberry.com/ns/widgets" version="2.0"
            rim:header="RIM-Widget: rim/widget">

            <rim:navigation mode="focus" />

  <name>DB1</name>
  <author rim:copyright="Copyright 2011-2012 Omni-Bridge Systems Pvt. Ltd."></author>
 
  <feature id="blackberry.io.file" version="1.0.0"/>
  <feature id="blackberry.io.dir" version="1.0.0"/>
 
  <content src="login.html"/>
   <icon src="homescreen.png"/>
   
 <feature id="blackberry.ui.dialog"/>
    
     <access uri="http://D-22"  subdomains="true">
             <feature id="blackberry.pim.memo" required="true" version="1.0.0"/>
             <feature id="blackberry.invoke.MemoArguments" required = "true" version="1.0.0"/>
     </access>
     
     <rim:connection timeout="45">
            <id>TCP_WIFI</id>
            <id>MDS</id>
            <id>BIS-B</id>
            <id>TCP_CELLULAR</id>
            <id>WAP2</id>
            <id>WAP</id>
      </rim:connection>
           
     
 
       <feature id="blackberry.ui.menu" required="true" version="1.0.0.0"/>
      <feature id="blackberry.app" required="true" version="1.0.0.0"/>
      <feature id="blackberry.invoke" required="true" version="1.0.0.0"/>
      <feature id="blackberry.invoke.BrowserArguments" required="true" version="1.0.0.0"/>
      <feature id="blackberry.invoke.MessageArguments" required="true" version="1.0.0.0"/>
      <feature id="extensions.screenshot" required="true" version="1.0.0.0"/>
      <feature id="blackberry.app.event" required = "true" version="1.0.0"/>
      <feature id="blackberry.system" />
      <feature id="blackberry.ui" />
      <feature id="blackberry.utils" />
        <feature id="blackberry.io.file" />
      
      <configuration
    xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
   <wsdl location="AirportWeather.wsdl" />
   </configuration>       
      
 
</widget>

 

----------------------------------------------------------------------------------------------------------------------

 

Also, guide me for more simple way to access webservice  through javascript.(without using Json Parser,DOMParser)

 

Please guide me for this.......!!!

 

Retired
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: How to call .NET web-service from javascript in Blackberry WebWorks Application

I moved this over to the Web Development Forum.. the Contributions forum is for WebWorks platform enhancement discussions based on the open source project in github

 

You shouldn't need the JDE 5.0 for WebWorks development, that will be for Java develoment only.

 

The <access> element specifies which origins/domains your application can access.. in your example you are trying to access a web service at http://192.168.1.179/

 

So your <access> element should look like: 

 

 

<access uri="http://192.168.1.179/" />

 

 

You also seem to have a <configuration> element at the bottom of your config.xml file.. You should remove this from the config.xml.

 

Have you checked out the following sticky link at the top of the forum for how to access a .NET web service using WebWorks?

 

http://supportforums.blackberry.com/t5/Web-Development/How-quot-I-quot-would-call-a-web-service-from...

 

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Highlighted
New Contributor
Posts: 6
Registered: ‎01-25-2011
My Device: Not Specified
My Carrier: Blackberry Application Developer

Re: How to call .NET web-service from javascript in Blackberry WebWorks Application

[ Edited ]

Hello Tim Sir,

                            Thanks for your quick response. You really cleared my doubt regarding <access> element in config.xml file. Also, I went through your link of how to access a .NET web service using WebWorks

http://supportforums.blackberry.com/t5/Web-Development/How-quot-I-quot-would-call-a-web-service-from...

 

I tried your code in my application & got  "null" value for jsonString

 var result = xmlHttp.responseXML.documentElement;
var jsonString = result.childNodes[0].nodeValue;

 

Also, here you have made use of JSON Parser. Do we need it for all applications accessing .net webservice. (I really didn't understand  its functioning). Could you suggest some more simpler way for it.?

 

 

Still there are issues from my side...   Now, in my login.html file

 

                     try
                     {
                        
                        var result = xmlhttp.responseXML;
                        alert(" result == "+result);     // here i get "[object HTMLDocument]"
                                             
                     
                     }

 

Does it mean that webservice is actually got accessed & i've received response. But rsponse doesn't show me contents of .xml file. Further, I am not able to access the actual response which the webservice returns. So, i tried out some other way for it .....

 

---------------------------------------------- gautam.html ---------------------------------------------

 

 

<html>
<head>

            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <meta name="viewport" id="viewport" content="height=device-height,width=device-width,user-scalable=no" />

<title> Web-Service </title>

</head>


<body onload="InitService()" >

 <form id="form1" name="start" method="post">


                <div id="divWSBehavior" style="behavior: url(webservice.htc)" onresult="onWSresult()">
                </div>
      
   
                <script type="text/javascript" language="JavaScript">
      
                  var iCallID;

 

                   function InitService()
                   {
                        alert("start InitService");
                        alert("initialise the webservice");
             divWSBehavior.useService("http://192.168.1.179/ms3s/DataService.asmx?wsdl","AuthenticateService");


             alert("call the webservice");
              iCallID=divWSBehavior.AuthenticateService.callService("Authenticate","obuser","obuser");
                                        
                    }
    

    function onWSresult()
    {
        alert("in result block");

        if((event.result.error)&&(iCallID==event.result.id))
        {
            var xfaultcode = event.result.errorDetail.code;
            var xfaultstring = event.result.errorDetail.string;
            alert("Error");
        }
        else
        {
           alert("in else");
            if (event.result.value == 0)
            {
                alert("1111");
                divWSBehavior.innerHTML = "Product By that name does not exist.";
            }
            else if(event.result.value == 0)
            {
                alert("2222");
                divWSBehavior.innerHTML = "Error: " + xfaultstring;
            }
            else
            {
                alert("we got the response");
                divWSBehavior.innerHTML = event.result.value;
            }
        }
    }
</script>
</form>
</body>

</html>


---------------------------------------------------------------------------------------------------------------

 

Is this approach correct? When i tried it on BB Simulator 9550 it  always shows me "RunTimeException".

 

But, when i tried it in IE 6.0 it clearly didplayed the response by the line :-

  divWSBehavior.innerHTML = event.result.value;

 

 

Please guide me for this.....