Ajax中async的設置問題

版權聲明:轉載請註明原創地址 https://blog.csdn.net/u013032788/article/details/37598679

問題描述:Ajax傳值狀態是200正確的,但就是獲取不到success後的返回值data,最後發現只有alert()一下才能夠正確獲取值。javascript

解決方法:把Ajax裏面的async設置爲false問題解決php

分析緣由:當async設置爲true時,是異步傳輸,即一js一邊傳向Server,一邊緊接着就執行下面的返回值,那若是Server那邊尚未處理好,這邊就固然不會有輸出值了,加上alert後會有是由於Server那邊已處理好,設置成false就是讓它執行完成後再執行html端的。html


//-----------------------------------前臺頁面--------------------------------------------------
java

<html>
<head>
<title></title>
<script type="text/javascript" src="./js/jquery.js"></script>
<script>
    function fun(){
        var username = $("input[name=username]").val();
        $.ajax({
            type: "GET",
            async: false,//默認是true
            url: "php.php",
            data: { username:username, Name: "Jquery" },
            success:function(data, st){
                alert(data);
                

            }jquery

          })
          
         // alert(username); 若是async設置爲true,在這個地方必需要alert一下才能夠獲取success裏的data值
        
    }
        

</script>

</head>
<body>
<div align="center">
<form action="" name="frm">
聯繫人:<input type="text" name="username" value="" /><br>
電&nbsp;&nbsp;話: <input type="text" name="phone" id="idPhone" /><br>
    <button onclick="fun();">提交</button><br>
    <div><span id="number">350</span>人報名</div>

</form>
</div>

</body>

</html>ajax

//-------------------------------------------服務器頁面---------------------------------------------------sql

<?php
include 'conn.php';
if(!empty($_GET['id']))
{
     echo $_GET['username'];
    
}

二當家的官網,請訪問 http://www.erdangjiade.com

更多商城企業源碼,盡在 http://www.erdangjiade.com/source

更多原創模板,盡在 http://www.erdangjiade.com/templates  

網頁特效下載:www.erdangjiade.com/js

PHP/Mysql:www.erdangjiade.com/php
     
PHP網站開發求職QQ羣 368848856

服務器

相關文章
相關標籤/搜索