使用ajax从数据库加载

时间:2014-04-03 05:25:14

标签: php jquery ajax

我运行的每个类型都会调用错误:OnError函数,我看不出它为什么不调用成功:OnSuccess,

JS:

$(document).ready(function () {
                // retreving data on button click
                $("#data-submit").click(LoadDataThroughAjaxCall);
                //loading screen functionality - this part is additional - start
                $("#divTable").ajaxStart(OnAjaxStart);
                $("#divTable").ajaxError(OnAjaxError);
                $("#divTable").ajaxSuccess(OnAjaxSuccess);
                $("#divTable").ajaxStop(OnAjaxStop);
                $("#divTable").ajaxComplete(OnAjaxComplete);
                //loading screen functionality - this part is additional - end
            });
            // ajax call
            function LoadDataThroughAjaxCall() {
                $.ajax({
                    type: "POST",
                    url: "Ajax/dataloader.php",
                    data: '{}',
                    dataType: "json",
                    success: OnSuccess,
                    failure: OnFailure,
                    error: OnError
                });
                // this avoids page refresh on button click
                return false;
            }
            // on sucess get the xml
            function OnSuccess(response) {
                //debugger;
                var xmlDoc = $.parseXML(response.d);
                var xml = $(xmlDoc);
                var tweets = xml.find("Table");
                showOnATable(tweets);
            }
            // show data on a table
            function showOnATable(tweets) {
                //debugger;
                var headers = [];
                var rows = [];
                // header section
                headers.push("<tr>");
                headers.push("<td><b>tweets</b></td>");
                headers.push("<td><b>created</b></td>");
                headers.push("<td><b>source</b></td>");

                headers.push("</tr>");
                // rows section
                $.each(tweets, function () {
                    var tweets = $(this);
                    rows.push("<tr>");
                    rows.push("<td>" + $(this).find("tweet_text").text() + "</td>");
                    rows.push("<td>" + $(this).find("created_at").text() + "</td>");
                    rows.push("<td>" + $(this).find("source").text() + "</td>");
                    rows.push("</tr>");
                });
                var top = "<table class='gridtable'>";
                var bottom = "</table>";
                var table = top + headers.join("") + rows.join("") + bottom;
                $("#divTable").empty();
                $("#divTable").html(table);
            }
            // loading screen functionality functions - this part is additional - start
            function OnAjaxStart() {
                //debugger;
                //alert('Starting...');
                $("#divLoading").css("display", "block");
            }
            function OnFailure(response) {
                //debugger;
                alert('Failure!!!' + '<br/>' + response.reponseText);
            }
            function OnError(response) {
                //debugger;
                var errorText = response.responseText;
                alert('Error!!!' + '\n\n' + errorText);
            }
            function OnAjaxError() {
                //debugger;
                alert('Error!!!');
            }
            function OnAjaxSuccess() {
                //debugger;
                //alert('Sucess!!!');
                $("#divLoading").css("display", "none");
            }
            function OnAjaxStop() {
                //debugger;
                //alert('Stop!!!');
                $("#divLoading").css("display", "none");
            }
            function OnAjaxComplete() {
                //debugger;
                //alert('Completed!!!');
                $("#divLoading").css("display", "none");
            }

PHP:

       <?php

//if(isset($_POST['data'])==true&&empty($_POST['data'])==false){
        require_once('../connection.php');
        function clean($str)
        {
        if(get_magic_quotes_gpc())
        {
            $str= stripslashes($str);
        }
        return str_replace("'", "''", $str);
        }

    //Sanitize the POST values
    //$username = clean($_POST['data']);
        //$result=sqlsrv_query($conn,"execute sp_ORDER_BY_name '$username'");
        $result=sqlsrv_query($conn,"select tweet_text,source from tweets");
        if($result) {
        if(sqlsrv_has_rows($result) > 0) {
            //Login Successful
            while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC) ) {
                 echo $row['tweet_text'].", ".$row['source']."<br />";

                }
        }else {
            //Login failed
             echo 'Name not found';

            }
        }
    //}
?>

HTML FORM:

 </head>
    <body>
        <div id="banner">
            <h1>P-CAT version 0.1</h1>
        </div>



        <div id ="content">
         <h2>Sreach  Catigroies</h2>
         <select id="data2">
         <option value="">Plece select one of the follwing</option>
          <option value="Name">Name</option>
          <option value="Location">Location</option>
        </select>
        <input name="data" id="data" type="text" />
        <input type="submit"  id="data-submit" value="Grab">



            <div id="divTable">    
            </div>
            </div>
              <div id="divLoading" style="display: none; position: absolute; top: 50%; left: 40%;
            text-align: left;">
            <span>
                <img src="Images/ajax-loader.gif"   alt="Image not found." /></span>
            <br />
            <span style="text-align: left; padding-left: 8px;">Loading ...</span>
             </div>

        <div id="navbar">
            <input type="button" value="EDIT">
            <input type="button" value="HISTORY">
            <input type="button" value="SETTINGS">
            <input type="button" value="SEARCH">
        </div>
     <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
     <script type="text/javascript" src="js/global.js"></script>
</body>

1 个答案:

答案 0 :(得分:0)

您必须response来自json的{​​{1}},

php