1. Home
  2. Docs
  3. JQUERY
  4. AJAX
  5. 等待回應ajax

等待回應ajax

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="httpsss://code.jquery.com/jquery-1.11.3.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
                $.ajax({
                    type: "GET",
                    url: '03-webservice.php',

                    data: {
                        "action": "01GetAllAndSubImagesFiles",
                        "username": "powenko",
                    },

                    async: false,   // 等待  async: false
                    cache: false,  // 清除 暫存
                    timeout: 10000, // 等10Sec, 10*1000 ms

                    success: function(response)
                    {
                        console.log(response);
                        var jsonData = JSON.parse(response);
                        if (jsonData.success == true)
                        {
                            console.log(response);
                            $("#powenko").html(response);
                        }
                        else
                        {
                            $("#powenko").html('Invalid Credentials!');
                        }
                    }
                });
        });
    </script>
</head>
<body>

<div id="powenko">

</div>
www.powenko.com
</body>
</html>

php webservice 程式

<?php
if (isset($_GET['action'])) {
    $action = isset($_GET['action']) ? $_GET['action'] : '';
    if ($action=="01GetAllAndSubImagesFiles") {
        $username = isset($_GET['username']) ? $_GET['username'] : '';


// sleep for 10 seconds
        sleep(10);
        $array2 = array(
            "success" => true,
            "username" => $username,
            "id" => 20200822,
        );
    }else{
        $array2 = array(
            "success" => false,
        );
    }
    echo  json_encode($array2);

} else {
    echo json_encode(array('success' =>false));
}