www.webdeveloper.com
Results 1 to 13 of 13

Thread: using a web service from javascript

  1. #1
    Join Date
    Jan 2011
    Posts
    8

    using a web service from javascript

    Hi all
    Thanks in advance for any help

    I have a webservice set up in asp as below.

    This webservice is running fine and seems to work ok.

    I also have a simple javascript page below.

    I cannot seem to get the Javascript page to post requests to the webservice.

    I have been fiddling with it for hours and just cannot seem to get it to work.

    I have adjust the firewall to accept all incoming and outgoing connection not that i think this would matter.

    Please could someone help. I would prefer not to go down the route of having and aspx page on the front end of my website as eventually i would like to replace my webservice with a php one but do not have php skills atm.

    The log i have set up in my vb webservice looks fine when using the firefox to invoke the service but my javascript does not seem to do anything.

    Anyway webservice is here.

    Imports System.Web
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.IO


    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    ' <System.Web.Script.Services.ScriptService()> _
    <WebService(Namespace:="http://tempuri.org/")> _
    <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    <System.Web.Script.Services.ScriptService()> _
    Public Class Service
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld(ByVal hell As String) As String

    Dim FILE_NAME As String = "C:\Webhosting1\asmxlog.txt"

    Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
    objWriter.WriteLine(hell)
    objWriter.Close()


    Return "Hello World smile" & hell

    End Function

    End Class

    and javascript is here


    <html>
    <script type="text/javascript" language="javascript">
    var myReq = new XMLHttpRequest();
    function callWSMethod1()
    {
    if (window.XMLHttpRequest)
    {
    var url = "http://localhost:61422/Webhosting1/Service.asmx?op=HelloWorld";

    myReq.onreadystatechange = CheckStatus1;

    myReq.open("GET", url, true); // true indicates asynchronous request

    myReq.send("smile");
    //alert("hello");
    //alert(myReq.responseText);
    }
    }
    function CheckStatus1()
    {
    if (myReq.readyState == 4) // Completed operation
    {
    myReq.open("POST","http://localhost:61422/Webhosting1/Service.asmx/HelloWorld", false);
    alert(myReq.responseText.toString());
    myReq.send();
    //alert(toString(myReq.responseText));
    form1.TextArea1.value = myReq.responseText;
    }
    //alert(myReq.responseText);
    }
    </script>
    <form id="form1" runat="server">
    <div>
    <input id="Button1" type="button" value="test" onclick="callWSMethod1() " /><br />
    <textarea id="TextArea1" name="S1"></textarea>
    </div>
    </form>
    <input id="Button2" type="button" value="Reverse The String" onclick="callWSMethod1() " /><br />
    </html>

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    I might be wrong, but I used to be convinced that one can not perform an AJAX request to the localhost.

  3. #3
    Join Date
    Mar 2009
    Posts
    430
    I might be wrong, but I used to be convinced that one can not perform an AJAX request to the localhost.

    I suppose that all tends to depend upon the circumstances. I develop om my own computer which runs Linux. When I am working on an AJAX project I start up Apache on my computer and have no problems sending or receiving AJAX, although I always use the format as in 'http://127.0.0.1/ajax.php'

  4. #4
    Join Date
    Jan 2011
    Posts
    8

    Tried already

    Thanks for the idea there.
    I had already tried replacing the localhost with the ip and it did not change anything.
    I have now tried running this from another box on my network and still no luck.
    I am new to the business of web services and guess that i have just over looked something simple like a setting somewhere on my box.
    Cheers in advance for any more assistance

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Tcobb View Post
    I might be wrong, but I used to be convinced that one can not perform an AJAX request to the localhost.

    I suppose that all tends to depend upon the circumstances. I develop om my own computer which runs Linux. When I am working on an AJAX project I start up Apache on my computer and have no problems sending or receiving AJAX, although I always use the format as in 'http://127.0.0.1/ajax.php'
    Sure, but if you open the web document with the proper address (like http://localhost/etc...) not straight.

    Ashw1984, Regarding the ASP.NET, well, I guess that you should use the IIS server emulator. And if you are working on Win7 as OS, well, things go a little bit tricky there... It might be a problem of the IIS configuration, rather...

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by Kor View Post
    I might be wrong, but I used to be convinced that one can not perform an AJAX request to the localhost.
    i just did in firefox 4. window.globalStorage was blocked from localhost, but i think localStrorage can work from localhost...

  7. #7
    Join Date
    Jan 2011
    Posts
    8

    My problem Solved ish...

    Hi guys

    I got it working Woop woop!!

    I missed this in my asmx web.config

    <webServices>
    <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
    </protocols>
    </webServices>

    Can't get any messages back yet but it is definatly contacting the webservice.

    Thanks for your time guys.

  8. #8
    Join Date
    Jan 2011
    Posts
    8

    Further issues

    Hi again guys

    Just wondered if anyone knows why my responseText from my xmlhttprequest would be blank.

    I am using the webservice exactly as it is earlier in this post. Running it through iis.

    The following script does get to the webservice ( i know this as i have logs in asmxlog.txt file (see webservice code)) but i cannot seem to get and data back. I have tried on the local box and a remote one and it has the same symptom. As you can see by my comments is have tried fiddling with it a lot.

    cheers in advance for assistance guys.


    <html>
    <script type="text/javascript" language="javascript">
    var myReq = new XMLHttpRequest();
    //var myReq = new ActiveXObject("MSXML2.XMLHTTP")
    function callWSMethod1()
    {
    if (window.XMLHttpRequest)
    {
    var url = "http://192.168.1.1:9090/Service.asmx/HelloWorld?hell=smile";
    //var url = "http://192.168.1.1:9090/Service.asmx?op=HelloWorld";

    myReq.open("GET", url, true); // true indicates asynchronous request

    //myReq.open("POST", url, true); // true indicates asynchronous request
    //myReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    //myReq.setRequestHeader("Content-length", 10);
    //myReq.setRequestHeader("Connection", "close");
    alert("hello");
    myReq.onreadystatechange = CheckStatus1;
    //myReq.send("hell=smelly");
    myReq.send();
    //alert(myReq.responseText);
    }
    }
    function CheckStatus1()
    {
    if (myReq.readyState == 4) // Completed operation
    {
    //myReq.open("POST","http://localhost:9090/Service.asmx/HelloWorld", false);
    //alert(myReq.responseXML);
    //alert(myReq.send().toString);
    alert(myReq.responseText);
    //alert(toString(myReq.responseXML));
    //form1.TextArea1.value = myReq.responseText;
    //alert(myReq.getAllResponseHeaders());
    //var url = "http://localhost:9090/Service.asmx"

    //myReq.onreadystatechange = CheckStatus1;
    //myReq.open("POST", url, true);
    //myReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    //var param=form1.TextArea1.value;
    //myReq.send("someText=param");
    }
    //alert(myReq.responseText);
    }
    </script>
    <form id="form1" runat="server">
    <div>
    <input id="Button1" type="button" value="test" onclick="callWSMethod1() " /><br />
    <textarea id="TextArea1" name="S1"></textarea>
    </div>
    </form>
    <input id="Button2" type="button" value="Reverse The String" onclick="callWSMethod1() " /><br />
    </html>

  9. #9
    Join Date
    Mar 2009
    Posts
    430
    Just to check, you might check the status of the request in your 'CheckStatus1()' function. The fact that the readyState equals 4 does not necessarily signify that it was successful, only that it is done. Substitute:
    Code:
    alert(myReq.status);
    for
    Code:
    alert(myReq.responseText);
    just to check. If status is anything other than 200 this will give you some idea of the nature of the error.

  10. #10
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    It is not enough to check the readyState property, you must check also the status
    Code:
    if (myReq.readyState == 4 && myReq.status == 200)

  11. #11
    Join Date
    Jan 2011
    Posts
    8

    Browser

    Hi guys thanks for the help

    Its half solved.

    I hav been dev'ng in firefox.

    It all works fine in IE.

    I assume there is some setting i need to change in firefox

    In IE i get the status of 200 and in firfox i get 0

    Thanks very much if you can help with this so i can knock it on the head.

    Cheers for all of the comments already

  12. #12
    Join Date
    Mar 2009
    Posts
    430
    For what its worth, my understanding is that you get status code zero when the AJAX request is trying to access a local file rather than going though a server. It would seem that FF does not recognize this in your situation, but that IE does.

  13. #13
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    who cares what the status actually is, you care about the content.

    just say:
    Code:
    if (myReq.responseText){
    and magically, it's only going to work when it should.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles