douyaju4749 2014-10-17 08:02
浏览 49
已采纳

无法检索POST数据

I am having trouble getting a basic ajax POST to work. I switched to an onclick after I was having trouble getting using a jquery .click, among other things. Just wondering if I am making some blatant mistake or what. If no obvious mistake, it may be something with apache? Not too much experienced here so any help would be appreciated.

Here is a link to a function:

<a href="markerpages.php" onclick="postData()">click this for php page</a>

Here is the function:

function postData() {
            console.log("outside ajax is working");

            $.ajax({
                type: "POST",
                url: "/markerpages.php",
                data: {
                    source1: "some text",
                    source2: "some text 2"},
                success: function (data) {
                    console.log("inside ajax is working");
                },
                error:  function () {
                    console.log("ajax post failed")
                }
            });

here is what I have on my php webpage:

<?php
 if (isset($_POST['source1'])) 
        $src1 = $_POST['source1'];
    else $src1 = "post data not obtained";

    echo $src1;

            echo "<pre>" . print_r($_REQUEST, 1) . "</pre>";

    print_r($_POST);
    var_dump($_POST);
    var_dump($_POST);die;
 ?>

I am not returning errors in firebug, and I am getting the log statements I placed inside ajax and outside, just not getting empty arrays on the PHP page. Sincere thanks for any help.

  • 写回答

3条回答 默认 最新

  • dpaal28266 2014-10-17 08:05
    关注

    You are sending the user to the page using the anchor tag. If you do this, all post data is lost. You need to replace the url in the anchor tag with # to make sure the user stays on the page:

    <a href="#" onclick="postData()">click this for php page</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题