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

报告相同问题?