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

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

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧