简体   繁体   English

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

[英]Post ajax data to php and then get the data not work

i've got some problem 我有问题

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

my jquery: 我的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代码:

<?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 最后,当ajax处理成功时,它将把服务器中的数据加载到容器div中

my question is, why it's gave me an error like this? 我的问题是,为什么会给我这样的错误?
"Notice: Undefined index: target in xxx.php on line 7" “注意:未定义的索引:第7行上xxx.php中的目标”
i know there must be something wrong with my data on my ajax, 我知道我在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 当我用查尔斯调试它时,ajax发送带有这样的文本字符串的数据
my POST request raw: 我的POST请求原始:

POST /xxx/dynamic.php HTTP/1.1 POST /xxx/dynamic.php HTTP / 1.1
Host xxx 主机xxx
Content-Length 67 内容长度67
Accept / 接受/
Origin http ://xxx 来源http:// xxx
X-Requested-With XMLHttpRequest X-Requested-With XMLHttpRequest
User-Agent xx Content-Type application/x-www-form-urlencoded; 用户代理xx内容类型应用程序/ x-www-form-urlencoded; charset=UTF-8 字符集= UTF-8
Referer http:/xxx.php 引荐来源网址:http:/xxx.php
Accept-Encoding gzip,deflate,sdch 接受编码gzip,deflate,sdch
Accept-Language en-US,en;q=0.8 接受语言en-US,en; q = 0.8

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

my POST response raw: 我的POST响应原始:

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

http ://xxx.php#productTest ajax http://xxx.php#productTest ajax

my GET request raw: 我的GET请求原始:

GET xxx.php HTTP/1.1 GET xxx.php HTTP / 1.1
Host: xxx 主持人:xxx
Accept: text/html, / ; 接受:text / html, / ; q=0.01 q = 0.01
X-Requested-With: XMLHttpRequest X-Requested-With:XMLHttpRequest
User-Agent: xx 用户代理:xx
Referer: http ://xxx.php 引荐来源:http://xxx.php
Accept-Encoding: gzip,deflate,sdch 接受编码:gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8 接受语言:en-US,en; q = 0.8

my GET response raw: 我的GET响应原始:

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

Notice : Undefined index: target in dynamic.php on line 2 注意 :未定义的索引:第2行上dynamic.php中的目标
Test ajax 测试ajax

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! 您不需要执行get ajax命令,只需要直接成功输出数据即可上面! :) :)

You aren't encoding the data in a format that PHP can decode and populate $_POST with. 您不会以PHP可以解码并填充$_POST的格式来编码数据。 You are sending a plain text string. 您正在发送纯文本字符串。

Change: 更改:

data: target,

to

data: { target: this.href },

When sending data via AJAX like you are, you have to replicate a form submission, eg 当像您一样通过AJAX发送数据时,您必须复制表单提交,例如

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

will send a bare string 'foo' over to the server. 会将裸字符串'foo'发送到服务器。 But having 但是有

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

you'll get the proper data for PHP to populate $_GET/$_POST for you. 您将获得适当的数据,以便PHP为您填充$ _GET / $ _ POST。 No key/value pair, no entry in $_GET/$_POST. 没有键/值对,在$ _GET / $ _ POST中没有条目。 It's that simple. 就这么简单。

Mate in your ajax definition shouldnt you pass the data as array? 您的ajax定义中的配合是否应该将数据作为数组传递? try changing the data: line with the below, should work for you: 尝试更改数据:与下面的行,应该适合您:

data: { target: the_varibable_you_want_to_pass_by_post }

EDIT: as per your comment and edited OP source and @Arthur answer 编辑:根据您的评论和编辑的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)
                 }
        })
      })

In your previous code, the "this" did not reffer to the clicked link but to the ajax object that does not have the 'href' attribute. 在您之前的代码中,“ this”不代表所单击的链接,而是不具有“ href”属性的ajax对象。

The problem is, that you don't send any data when making the secong request: 问题是,发出第二请求时您不发送任何数据:

$(".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)
  })
});

So at first it works, but when performing a second request, PHP can't find the 'target' because you are not sending it. 因此,起初它可以工作,但是当执行第二个请求时,PHP找不到“目标”,因为您没有发送它。

And it's better to cache the href first: 而且最好先缓存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);
        })
    });
});

Or another solution is to check if the "target" exists in PHP: 或者另一个解决方案是检查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";
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM