duangu1645
2013-09-09 14:09
浏览 244
已采纳

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

i've got some problem

my html: http://jsfiddle.net/dHdnb/

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

})

here is my php code:

<?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";
?>

lets me explain this, if the user click the button on those list
then, it will post the target variable into the server
then, the server will process the request and launch a function
finally, when the ajax process success, it will load the data from the server into the container div

my question is, why it's gave me an error like this?
"Notice: Undefined index: target in xxx.php on line 7"
i know there must be something wrong with my data on my ajax,
but i don't know where's my mistakes
please help me :)

when i'm debug it with charles, the ajax send the data with text string like this
my POST request raw:

POST /xxx/dynamic.php HTTP/1.1
Host xxx
Content-Length 67
Accept /
Origin http ://xxx
X-Requested-With XMLHttpRequest
User-Agent xx Content-Type application/x-www-form-urlencoded; charset=UTF-8
Referer http:/xxx.php
Accept-Encoding gzip,deflate,sdch
Accept-Language en-US,en;q=0.8

target=http%3A%2F%2Fxx%2Fxx%2Fhome.php%23product

my POST response raw:

HTTP/1.1 200 OK
Date: Tue, 10 Sep 2013 09:35:19 GMT
Server: Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16
X-Powered-By: PHP/5.4.16
Content-Length: 57
Content-Type: text/html

http ://xxx.php#productTest ajax

my GET request raw:

GET xxx.php HTTP/1.1
Host: xxx
Accept: text/html, /; q=0.01
X-Requested-With: XMLHttpRequest
User-Agent: xx
Referer: http ://xxx.php
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

my GET response raw:

HTTP/1.1 200 OK
Date: Tue, 10 Sep 2013 09:45:43 GMT
Server: Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16
X-Powered-By: PHP/5.4.16
Content-Length: 152
Content-Type: text/html

Notice: Undefined index: target in dynamic.php on line 2
Test ajax

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

5条回答 默认 最新

  • dqybjj3497 2013-09-11 10:50
    最佳回答

    There is the valid code!

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

    you don't need to do the get ajax command, you just need to directly output the data in the success: above! :)

    评论
    解决 无用
    打赏 举报
查看更多回答(4条)

相关推荐 更多相似问题