普通网友 2015-01-31 11:01
浏览 27
已采纳

PHP页面不会使用接受Jquery POST和GET变量

The code on my php:

<html>
    <script src="jquery2_3.js"></script>

<script>

function ajax(){
    $.ajax({
        type: 'POST',
        url: 'mypage.php',
        data: {test: 'asdf'},
        success: function(){
            alert('done');
        }
    });
}

</script>
</html>

<?php

foreach ($_GET as $key => $value) {
        echo "<tr>";
        echo "<td>";
        echo $key;
        echo "</td>";
        echo "<td>";
        echo $value;
        echo "</td>";
        echo "</tr>";
    }

?>

When I use PostMan (Chrome app REST Client) I can submit GET variables and get the expected results echoed on the page.

When I open this page in Chrome and run the ajax method via js console, I get the alert message, but nothing echoed from the php script or any changes on the page.

I have tried using using the jquery get() and post() methods both which indicated success but no results displayed. But the php code runs fine when I send the requests through Postman.

Jquery gives me no errors, any ideas? Thanks

  • 写回答

2条回答 默认 最新

  • dongtou8736 2015-01-31 11:16
    关注

    Ok, I realized my mistake right once I posted this.

    The problem is that I'm confusing how serverside and client side processes work. When the ajax method is ran that can make changes to the client side but the php server cannot.

    Solution is to capture the echoed data in the success callback and append that with jquery:

    $.ajax({
            type: 'POST',
            url: 'mypage.php',
            data: {test : '32452345'},
            success: function(data){
                $('*').html(data);
            }
        });
    
    <?php
    
    echo $_POST['test'];
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大