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 Developer
Posts: 5
Registered: ‎07-23-2008
My Device: Not Specified
Accepted Solution

Web Service is not Working through XMLhttprequest

Good Evening,

 

I am having an issue with calling a web service from Javascript for my Web Works application.  I believe it has something to do with url of the web service but could use anyone's help.  First off the Service Works fine on my development machine (using Visual Studio to test it through Javascript without a Web Works application).

 

-Windows XP SP2

-ASP.Net Webservice located on localhost.

-Eclipse IDE for Java Developers (just downloaded today)

Version: Helios Release

Build id: 20100617-1415

 

Javascript Code:

 

 

        var xmlHttp;
        
        if (window.XMLHttpRequest)     // Object of the current windows
        { 
            xmlHttp = new XMLHttpRequest();     // Firefox, Safari, ...
        } 
        else 
        if (window.ActiveXObject)   // ActiveX version for IE5 and IE6
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
        } 
        
        //var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        
        function callService()
        {
            var url = "http://localhost/AJAXEnabledWebsite1/WebService/WebService.asmx?op=HelloWorld";
            try
            {
                xmlHttp.onreadystatechange = useHttpResponse;
                xmlHttp.open('GET', url, true);
                xmlHttp.send(null);
            }
            catch (e)
            {
                document.getElementById('txtResult').value = e;
            }
        }
        
        function useHttpResponse() 
        {
            
          if (xmlHttp.readyState == 4) 
          {           
              if(xmlHttp.status == 200)
              {   
       document.getElementById('txtResult').value = xmlHttp.responseXML.xml;
              }
              else 
              {
                    document.getElementById('txtResult').value = "Error code " + xmlHttp.status;
              }
            }
        }
Web Service Code:
using System;
using System.Data.SqlClient;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
namespace HelloWorldApplication
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService()]
    public class WebService : System.Web.Services.WebService
    {
        public WebService()
        {
            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }
        [WebMethod]
        public string HelloWorld()
        {
            return "Web Service Hello World";
        }
...
I also added the info below to the web config
<system.web>
    <webServices>
      <protocols>
        <add name="HttpSoap"/>
        <add name="HttpGet"/>
      </protocols>
    </webServices>
I did a little testing and I get an error on this piece of code "xmlHttp.open('GET', url, true);"
Can anyone help me out?

 

New Developer
Posts: 5
Registered: ‎07-23-2008
My Device: Not Specified

Re: Web Service is not Working through XMLhttprequest

Update

 

Having limited success on the Simulator using Visual Studio application and not web works application.

Developer
Posts: 125
Registered: ‎01-17-2011
My Device: Playbook
My Carrier: Vodacom

Re: Web Service is not Working through XMLhttprequest

[ Edited ]

 

Hi

Got it to work on my simulator (beta2) calling a webservice I was running and debugging in VisualStudio 2010.

You code looks similar to the most  - except:
   - haven't got the ActiveXObject code
   - I specify the MimeContent e.g. ('application/x-www-form-urlencoded')
     see sample code below ...
   - I set access permissions in config.xml
       e.g. :     <access uri="*" subdomains="true" />
       e.g. :     <access uri="http://localhost" subdomains="true" />
       e.g. :     <access uri="http://mydomain.com" subdomains="true" />

   - also check xml.responseText which might be set instead of responseXML
      if your mimetypes aren't correct

 

Sample Call:

    var url = "http://localhost/TestWebService1/service.asmx/HelloWorld";
    xmlHttp.open('GET', url, true);
    xmlHttp.onreadystatechange = callbackFunction;
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send(null);

 

   

 

 

 

Sample Callback:

   function callbackFunction() {
       alert("callbackFunction enter");

       if (xmlHttp.readyState != 4)
           return;

       alert("callbackFunction response hdrs - " + xmlHttp.getAllResponseHeaders());
       alert("callbackFunction response text - " + xmlHttp.responseText);
       alert("callbackFunction response xml - " + xmlHttp.responseXML);

       // ....
       // ....
   }  

 

 


Hope that helps

.H

 



 

 


New Developer
Posts: 5
Registered: ‎07-23-2008
My Device: Not Specified

Re: Web Service is not Working through XMLhttprequest

Thank you for you're help - it is now working correctly.  It was a comedy of errors on my part.