Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    calling webservices from ajax not invoking method

    Hello,
    Please go easy on me because i am a fresh man.
    I have a web service running on my local machine with this url;
    http://localhost:28553/WebSite7/Service.asmx?
    There is so far only one method; HelloWorld which takes in a string and returns another string back.
    I also have an html page running javascript and ajax and i am trying to consume the webservice but the problem is instead of invoking the method i am getting the test page of the webservice that shows the methods being returned to me. Below is the code that i am using.

    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function loadXMLDoc()
    {

    var xmlhttp;
    xmlhttp = new XMLHttpRequest();


    xmlhttp.open("GET","http://localhost:28553/WebSite7/Service.asmx?op=HelloWorld",false);


    xmlhttp.send("input=HELLO");


    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

    }
    </script>

    </head>
    <body>
    <div id="myDiv">
    <h2>Let AJAX change this text</h2>
    <button type="button" onclick="loadXMLDoc()">Change Content</button>
    </div>
    </bodt>
    </html>


    Please , please i need assistance. I am stuck on a major project if i don't get this right. I got this far by following certain examples that i found on the net and i now i need the help of experts.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,686
    Thanks
    25
    Thanked 656 Times in 655 Posts
    Only see two problems and two things that I will comment on.

    First your last body tag is misspelled </bodt> S/B </body>

    and xmlhttp.send("input=HELLO"); I believe this is always left empty:
    xmlhttp.send(); or xmlhttp.send("");

    ------------------
    Comments
    xmlhttp = new XMLHttpRequest(); is fine if you don't need this to work with I.E.

    I'd move the </div> up to leave the button on the screen and only change the statement, Let AJAX change this text.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by sunfighter View Post
    and xmlhttp.send("input=HELLO"); I believe this is always left empty:
    xmlhttp.send(); or xmlhttp.send("");
    Yes but only for GET method
    POST method may use parameters

    from w3school

    Code:
     
     xmlhttp.open("POST","ajax_test.asp",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("fname=Henry&lname=Ford");
    http://www.w3schools.com/ajax/ajax_x...quest_send.asp

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,471
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by igimanigi View Post
    I have a web service running on my local machine with this url;
    http://localhost:28553/WebSite7/Service.asmx?
    Unless the page with the AJAX script also runs under http://localhost:28553/, the AJAX is subject to the Same-Origin-Policy (i.e. refuses to connect).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    These are the changes that i have made;


    xmlhttp.open("POST","http://localhost:28553/WebSite7/Service.asmx?op=HelloWorld",false);

    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("input=HELLO");


    as well as the </body> which i had miss typed.
    Now when i click the button i get a very long error and the following is only part of it;

    soap:ReceiverSystem.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String arg) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlTextReader.Read() at

    If i change from POST to GET i dont get this error but then the original problem returns where clicking the button simply displays the text on the web service Test page instead of actually invoking the method.

    Any ideas guys;

  • #6
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Dormilich can you explain further. so far i have changed from GET to POST like others have suggested and i have corrected my spelling mistake on </body> but now i get a very lengthy error;

    soap:ReceiverSystem.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String arg) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlTextReader.Read() at

    Can you please explain about the same origin policy you are talking about. how to i fix that.

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,471
    Thanks
    13
    Thanked 361 Times in 357 Posts
    those are two different issues. the exception is about invalid XML. and for the SOP you find a summary here.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have done a bit of research and i have come this far;
    first of all this is my jQuery code;

    <!DOCTYPE html>
    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
    </script>
    <script>

    $(document).ready(function(){
    $("button").click(function(){

    $.ajax({
    type: "POST",
    crossDomain: true,
    contentType: "application/json; charset=utf-8",
    url: "http://localhost:29114/WebSite9/Service.asmx/HelloWorld",
    data: "{}",
    dataType: "jsonp",
    successnSuccess,
    errornError});

    });

    });

    function onSuccess()
    {
    alert("success");
    }

    function onError()
    {
    alert("error");
    }
    </script>
    </head>
    <body>

    <div><h2>Let AJAX change this text</h2></div>
    <button>Change Content</button>

    </body>
    </html>

    and this is my asp .net code for the HelloWorld service being invoked;

    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public void HelloWorld() {

    StringBuilder sb = new StringBuilder();
    JavaScriptSerializer js = new JavaScriptSerializer();
    sb.Append("fn(");
    sb.Append(js.Serialize("Hello World"));
    sb.Append(");");

    Context.Response.Clear();
    Context.Response.ContentType = "application/json";
    Context.Response.Write(sb.ToString());
    Context.Response.End();
    }


    Unfortunately my javascript code keeps coming out on the onError function. I am not sure where i am falling short now. please assist me on either the javascript or on the asp.net code.

  • #9
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have managed to use chrome dev to debug and here is what i have noticed. that my c# function is returning the data very fine because i can actually see the value of the callback function. but now my jquery function i think its not able to parse it because it keeps coming out on the onError part. but then according to chrome dev its a successful process. i have varied my output from my webservice as follows;
    1. {name:"hello"};
    2. {"name":"hello"}
    3. myFunction{"name":"hello"};

    but all these outputs i am not able to handle them. I have also noticed using chrome dev that when i load the script it runs 3 connections to jquery first before i click my run button. one of those requests that it makes to jquery is with this url;

    http://ajax.googleapis.com/ajax/libs...jquery.min.map

    i checked what data is returned by that. then i took this url and used it in place of mine on my jquery call just to see if i get the same data back. and yes, i get the same data back as what is happening in the first 3 calls when the script is still loading but only i am still not able to parse the response. the message i get is code 200 and message success.
    So can someone help me with where i am. i am not able to handle the returning data. please please i need help. i have been on this for almost a week.

  • #10
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello Moderator. I think it helps if we get an explanation why our posts are not being displayed.

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by sunfighter View Post
    Comments
    xmlhttp = new XMLHttpRequest(); is fine if you don't need this to work with I.E.
    XmlHttpRequest is supported in IE7+. Only IE6 and below need ActiveXObject for AJAX.

    igimanigi, try loading http://localhost:29114/WebSite9/Service.asmx/HelloWorld manually in the browser if it works.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #12
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,686
    Thanks
    25
    Thanked 656 Times in 655 Posts
    I think we went through that before. OK, thanks, nice to know and will shorten HTML code.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

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