Why isn't ajax button not firing and interpreting the return value?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Johnson
    New Member
    • Oct 2010
    • 97

    Why isn't ajax button not firing and interpreting the return value?

    Hi,

    Running this one in VS2010 with Firefox as my default browser.

    I have a very simple aspx page which connects to a webservice. The webservice is working correctly (which is a start).

    First problem is that the button is not firing in the jQuery. If I wrap a simple alert() around the button and click the button, the alert pops up. I've put an alert in the jQuery and nothing is happening, so I'm presuming the event is not happening.

    Second problem is interpretting the results. The webservice returns a ListItem. How do I tell the jQuery to return the result in resultsarray defined at the top of the JS?

    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>A quick and dirty hack</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
    
            var returnval = array(4);
            var server = "PB5\SQLEXPRESS";
            var db = "foobar\data";
            var cmds = "Select name from data where (age > 25 and hometown = 'frodsham');";
    
            $(document).ready(function () {
                $("#btnMore").click(function () {
                alert("In click function");
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        url: "WebService1.asmx\GetData",
                        data: "{'server':" + server + ",'db':" + db + ",'cmds':" + cmds + "}",
                        success: onSuccess,
                        error: onError
                    });
                });
            });
    
            function onSuccess(result) {
                alert("In onSuccess");
                $("#datalist").empty();
                var strings = result.d;
                for (var i = 0; i < strings.length; ++i)
                    $("#datalist").append("<li>" + strings[i] + "</li>");
            }
    
            function onError(result) {
                alert(result.status + ": " + result.statusText);
            }
    
        </script>
    </head>
    <body>
    <form id = "form1" runat="server">
    <asp:ScriptManager ID="_scriptManager" runat="server">
      <Services>
        <asp:ServiceReference Path="WebService1.asmx" />
      </Services>
    </asp:ScriptManager>
    
    <div>
    <ul id="datalist"></ul>
    <br />
    <input type="button" id="btnMore" value="do it" />
    </div>
    
    </form>
    </body>
    </html>
    Thanks

    Paul
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Check the jQuery.ajax() function options. You probably want to set processData to false.

    For the second problem, you expect json, so parse the response using a JSON parser. Make sure the webservice is returning valid JSON.

    Comment

    Working...