普通网友 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条)

报告相同问题?

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表