search

Home  >  Q&A  >  body text

ajax $.get request php unstable - Stack Overflow

html file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <h3>问卷调查</h3>
    <form action="#" id="forme">
        <p>姓名:<input type="text" id="username" name="username"></p>
        <p>性别:<input type="text" id="sex" name="sex"></p>
        <button id="send">提交</button> 
    </form>
    
    <hr>
    <!-- 内容展示区域 -->
    <p class="show">
    </p>
</body>
<script src="../jquery.js"></script>
<script type="text/javascript">
    $(function() {
        $('#send').click(function() {
            // $.get(url, args, callback, type)
            $.get(
                // url
                'ser.php',
                
                // args参数
                // { username:$('#username').val(),sex:$('#sex').val()},
                $('form').serialize(),
                
                // 载入数据成功 回调函数 
                function(data, textStatus) {
                    // 填入数据data
                    console.log(data, textStatus)
                    $('p.show').html(data)
                }
            )
        })
    })
</script>
</html>`

php file

<?php
  header("Content-Type:text/html; charset=utf-8");
  echo "
    <h3>姓名:{$_REQUEST['username']}</h3>
    <p>性别:{$_REQUEST['sex']}</p>
  ";
?>

Question 1: The callback function console.log(data) can be printed, but $('p.show').html(data) this step runs successfully The probability is very low, and every time it happens, it's like having a convulsion.

Question 2: In the console network (Google), the requested ser.php is not always available. Trouble...

Kneel down and beg...

阿神阿神2718 days ago610

reply all(4)I'll reply

  • 为情所困

    为情所困2017-06-05 11:09:32

    I tested it with the code you wrote. There is no problem and there is no problem at all. That's strange!

    reply
    0
  • 高洛峰

    高洛峰2017-06-05 11:09:32

    Check PHP logs.

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-05 11:09:32

    The code looks fine and actually runs fine.

    It is recommended to check the browser’s proxy, plug-ins, etc. and the current network environment

    If it is a development environment, you can try to change the development environment for testing. In the production environment, please check whether there are any problems with the system configuration, firewall, etc.

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-05 11:09:32

    Console network (Google), the requested ser.php is not always available.

    Get request, if the address requested each time is the same, the browser will cache it. So in order to make the request possible every time, the usual approach is to add a random number to the request parameter, such as url:xxx.com?a=1&r=Math.radom()

    reply
    0
  • Cancelreply