将ajax数据发布到php然后获取数据不起作用

时间:2013-09-09 14:09:18

标签: php jquery ajax

我遇到了一些问题

我的HTML: http://jsfiddle.net/dHdnb/

我的jquery:

$(".header_nav li a").click(function(){
  var href = this.href;
  $.ajax({
  url: 'dynamic.php',
  type: 'POST',
  data: { target: href },
  success: setTimeout(function(){
           $.ajax({
           url: 'dynamic.php',
           dataType: 'html',
           data: { target: href},               
           success: function(data) {
                    $(".container").html(data)
                    }           
           })
           }, 1000)

})

这是我的PHP代码:

<?php
  $target = $_POST["target"];
  echo $target;

  function home(){
  echo $target;
  // some command
  }
  switch($target) {
  case "home": home();
  break;
  // and so on
  default;
  }

  $target = isset($_POST['target']) ? $_POST['target'] : 'default_target_value';
  echo $target;

  echo "Test ajax";
?>

让我解释一下,如果用户点击这些列表上的按钮
然后,它会将目标变量发布到服务器中 然后,服务器将处理请求并启动功能
最后,当ajax进程成功时,它会将数据从服务器加载到容器div中

我的问题是,为什么它给了我这样的错误?
“注意:未定义的索引:第7行的xxx.php中的目标”
我知道我的ajax上的数据一定有问题,
但我不知道我的错误在哪里 请帮帮我:)。

当我用charles调试它时,ajax发送带有文本字符串的数据,如下所示 我的 POST 请求raw:

  

POST /xxx/dynamic.php HTTP / 1.1
  主持人xxx
  内容长度67
  接受 /
  来源http:// xxx
  X-Requested-With XMLHttpRequest
  用户代理xx   Content-Type application / x-www-form-urlencoded;字符集= UTF-8
  Referer http:/xxx.php
  接受编码gzip,deflate,sdch
  Accept-Language en-US,en; q = 0.8

     

目标= HTTP%3A%2F%2Fxx%2Fxx%2Fhome.php%23product

我的 POST 响应raw:

  

HTTP / 1.1 200 OK
  日期:2013年9月10日星期二09:35:19 GMT
  服务器:Apache / 2.4.4(Win32)OpenSSL / 0.9.8y PHP / 5.4.16
  X-Powered-By:PHP / 5.4.16
  内容长度:57
  内容类型:text / html

     

http://xxx.php#productTest ajax

我的 GET 请求raw:

  

GET xxx.php HTTP / 1.1
  主持人:xxx
  接受:text / html, / ; Q = 0.01
  X-Requested-With:XMLHttpRequest
  用户代理:xx
  推荐人:http://xxx.php
  Accept-Encoding:gzip,deflate,sdch
  Accept-Language:en-US,en; q = 0.8

我的 GET 响应raw:

  

HTTP / 1.1 200 OK
  日期:2013年9月10日星期二09:45:43 GMT
  服务器:Apache / 2.4.4(Win32)OpenSSL / 0.9.8y PHP / 5.4.16
  X-Powered-By:PHP / 5.4.16
  内容长度:152
  内容类型:text / html

     

注意 2 dynamic.php 中未定义的索引:目标   测试ajax

5 个答案:

答案 0 :(得分:7)

有有效的代码!

var href = this.href;
        $.ajax({
        url: 'dynamic.php',
        type: 'POST',
        data: { target: href },
        success: function (data) {
                    setTimeout(function (){
                    $(".container").html(data)
                    }, 1000)
                }
        });

您不需要执行获取ajax 命令,只需要在成功上面直接输出数据! :)

答案 1 :(得分:0)

您不是以PHP可以解码和填充$_POST的格式对数据进行编码。您正在发送纯文本字符串。

变化:

data: target,

data: { target: this.href },

答案 2 :(得分:0)

当您通过AJAX发送数据时,您必须复制表单提交,例如

$.ajax(...
   data: 'foo'
);

会将一个字符串'foo'发送到服务器。但是有了

$.ajax(...
    data: 'bar=foo'; // key=value pair
    // OR
    data: {bar: 'foo'} // javascript key/value object
);

您将获得PHP的正确数据,以便为您填充$ _GET / $ _ POST。没有键/值对,$ _GET / $ _ POST中没有条目。就这么简单。

答案 3 :(得分:0)

你的ajax定义中的配合不应该将数据作为数组传递吗?尝试更改数据:下面的行,应该适合你:

data: { target: the_varibable_you_want_to_pass_by_post }

编辑:根据您的评论和编辑的OP来源和@Arthur回答

 $(".header_nav li a").click(function(){
     var clicked_link = $(this); //the variable stores the refference to the clicked "a" element
        $.ajax({
        url: 'dynamic.php',
        type: 'POST',
        data: { target: clicked_link.href }, //so now you can reffer to the href of the "a"
                 success: $.ajax({
                 url: 'dynamic.php',
                 type: 'POST',
                 data: { target: clicked_link.href },
                 dataType: 'html',                
                 success: function(data){
                 $(".container").html(data)
                 }
        })
      })

在您之前的代码中,“this”没有响应所点击的链接,而是没有“href”属性的ajax对象。

答案 4 :(得分:0)

问题是,在发出secong请求时你没有发送任何数据:

$(".header_nav li a").click(function(){
  $.ajax({
  url: 'dynamic.php',
  type: 'POST',
  data: { target: this.href }, //i can see the data here
  success: $.ajax({
           url: 'dynamic.php', //notice the same PHP script
           dataType: 'html', // where is the data in this request?               
           success: setTimeout(function(data){
           $(".container").html(data)
           }), 1000)
  })
});

首先它起作用,但是当执行第二个请求时,PHP找不到'目标',因为你没有发送它。

最好首先缓存href:

$(".header_nav li a").click(function() {
    var href = this.href;
    $.ajax({
        url: 'dynamic.php',
        type: 'POST',
        data: {
            target: href //data for first request
        },
        success: $.ajax({
            url: 'dynamic.php',
            dataType: 'html',
            data: {
                target: href //data for the second request
            },        
            success: setTimeout(function(data) {
                $(".container").html(data)
            }),
            1000);
        })
    });
});

或另一种解决方案是检查PHP中是否存在“目标”:

<?php
    $target = isset($_POST['target']) ? $_POST['target'] : 'default_target_value';

    function home(){
        // some data like echo and so on
    }

    switch($target) {
      case "home": home();
          break;
          // and so on
          default;
    }
    echo "Test ajax";
?>