ajax $.get request php unstable - Stack Overflow
阿神
阿神 2017-06-05 11:07:32
0
4
612

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...

阿神
阿神

闭关修行中......

reply all(4)
为情所困

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

小葫芦

Check PHP logs.

过去多啦不再A梦

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.

淡淡烟草味

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()

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template