【问题标题】:AJAX isn't sending data correctlyAJAX 未正确发送数据
【发布时间】:2013-04-26 09:34:59
【问题描述】:

我正在尝试将一些 POST 数据 AJAX 到 PHP 页面,但数据未正确发送。我做错了什么?

这是我的代码:

HTML

<a id="some_id">LINK</a>

Ajax 函数

mFunction(){    
    $("#some_id").click(function(){
        var thisId = $(this).attr('id');
        console.log(thisId);
        $.ajax({
        type : 'POST',
        url : 'magic.php',
        data: {"thisId" : thisId},
        dataType: "json",
        success:function(data){
        console.log(data);
       }
     });    
  });  
}

PHP

<?php$thatId = $_POST['thisId']; print_r($_POST); ?>

所以一切都应该按照我的理解进行,但是有问题。 在 console.log(data) 我获取 ID 所以数据已经发送 但是在 print_r 我得到一个 () 空数组的数组..

【问题讨论】:

  • 而PHP部分在哪里?
  • &lt;?php$thatId = $_POST['thisId']; print_r($_POST); ?&gt;
  • 您确定Pvariables_order 的一部分吗?

标签: php jquery html ajax


【解决方案1】:

magic.php

echo json_encode($_POST);

【讨论】:

    【解决方案2】:

    你有dataType: "json",,所以你的ajax调用期待json响应,这就是为什么你没有看到任何响应。

    使用json_encode();

    echo json_encode($_POST);
    

    【讨论】:

    • 我不认为这是问题所在;我相信 OP 说$_POST 是空的。
    • @Jack 我知道,但是您可以看到 OP 使用 POST 类型并正确发布数据,所以这可能是导致...如果还有其他任何事情请告诉我,以便我更正它..
    • 你的意思是什么?我输入 PHP echo json_encode($_POST); $pageid = $_POST['linkId2'];
    • @Froxz 那么发生了什么,它起作用了..???我认为它的thisId 不是linkId2
    • @Froxz 你看到我之前关于variables_order的评论了吗?
    【解决方案3】:

    我认为这行:

    data: {"thisId" : thisId},
    

    应该是

    data: {thisId : thisId},
    

    【讨论】:

    • @Jack dataType 是您期望从服务器返回的数据类型,但 data 只能包含纯对象或字符串,我错了吗??
    • 是的,对象或字符串。在这种情况下,它是一个对象;对象文字中的属性名称可以被引用(JSON 标准),也可以不被引用。
    【解决方案4】:

    魔法.php

    echo json_encode($_POST);
    

    html.html

     mFunction(){    
       $("#some_id").click(function(){
         var thisId = $(this).attr('id');
         console.log(thisId);
         $.ajax({
         type : 'POST',
         url : 'magic.php',
         data: {thisId: thisId},
         dataType: "json",
         success:function(data){
         console.log(data);
         }
        });    
      });  
    }
    

    【讨论】:

      【解决方案5】:

      如果$_POST 为空(这里似乎就是这种情况),您应该查看您的配置文件,尤其是variables_order 设置。

      例如,如果 variables_order 设置为"SP",那么 PHP 将创建超全局变量 $_SERVER 和 $_POST,但不会创建 $_ENV、$_GET 和 $_COOKIE。设置为 "" 意味着不会设置超全局变量。

      确保"P" 是此设置的一部分,即

      variables_order = "GPCS"
      

      进行此更改后重新启动服务器。

      【讨论】:

      • 不能那样做.. success:function(data){ console.log(data); } 我收到对象 {link:"The_pressed_ID"} 但它成功了,所以它应该可以工作(((
      • @Froxz 你说你看到了空数组;此外,您在问题中发布的 PHP 代码似乎并没有真正给出该结果。
      【解决方案6】:

      当你向服务器请求参数时必须是对象,

      数据:

      {thisId: "abc"}

      不是:

      {"thisId": "abc"}

      在这种情况下,为什么不给 thisId var 取一个不同的名称?

      var thisId = $(this).attr('id');

      var tId = $(this).attr('id');

      并使用它:

      $.ajax({
           type : 'POST',
           url : 'magic.php',
           data: {thisId: tId},
           dataType: "json",
           success:function(data){
           console.log(data);
           }
          });
      

      它与传递给关键帖子的对象名称一致,我不确定它是否可以重复,但我不会选择相同的名称。

      【讨论】:

      • {thisId: thisId} 可以使用。
      猜你喜欢
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      相关资源
      最近更新 更多