简体   繁体   English

jQuery AJAX PHP JSON问题

[英]jQuery AJAX PHP JSON problem

I'm facing the problem of receiving an empty array when I do an AJAX request in the following way: 当我以下列方式执行AJAX请求时,我遇到了接收空数组的问题:

This is the code I'm executing in JavaScript: 这是我在JavaScript中执行的代码:

  <script type="text/javascript" src="lib/jquery.js"></script>
  <script type="text/javascript" src="lib/jquery.json.js"></script>
  <script type="text/javascript">   
   $(document).ready(function(){

    /* Preparar JSON para el request */
    var mJSON = new Object;
    mJSON.id_consulta = new Array;
    for (var i=0; i<3; i++){
     mJSON.id_consulta[i] = new Object;
     mJSON.id_consulta[i].id = i;
    }
    var sJSON = $.toJSON(mJSON); 

    $.ajax({
     type: "POST",    
     url: "getUbicaciones.php",  
     data: sJSON, 
     dataType: "json", 
     contentType: "application/json; charset=utf-8",              
     success: function(respuesta){  
      alert(respuesta);
     },
     error: function (request,error){
      alert("Error: " + request.statusText + ". " + error);
     }
    });  

   });
  </script>

And this is the code under PHP: 这是PHP下的代码:

 <?php 
 /* Decodificar JSON */
 $m_decoded = $_POST;

 print_r($m_decoded);
 exit;
 ?>

And all I get from this, using Chrome's Developer Tools is an empty array: 我从中得到的是,使用Chrome的开发人员工具是一个空数组:

Array
(
)

Any clues on what am I doing wrong? 关于我做错了什么的线索?

The string sJSON is being encoded correctly, this is what I get when I do an "alert" on that one: 字符串sJSON正在被正确编码,这是我在那个上做“警告”时得到的:

{"id_consulta":[{"id":1},{"id":2},{"id":3}]}

Thank you everyone in advance! 提前谢谢大家!

From your JavaScript, you need to pass the data like this, as key-value pairs: 从您的JavaScript中,您需要将这样的数据作为键值对传递:

data: {"mydata" : sJSON},

On the PHP side, since $_POST is an associative array you can then access your data like so: 在PHP方面,因为$ _POST是一个关联数组,你可以像这样访问你的数据:

$m_decoded = $_POST['mydata'];

You're not decoding JSON on PHP-side. 你不是在PHP端解码JSON。

Try json_decode 试试json_decode

There are several issues in your code: 您的代码中存在几个问题:

  1. You are declaring dataType: "json" but the server does not return JSON, it returns plain text. 您正在声明dataType: "json"但服务器不返回JSON,它返回纯文本。 From the documentation : 文档

    The type of data that you're expecting back from the server. 期望从服务器返回的数据类型。 If none is specified, jQuery will intelligently try to get the results, based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). 如果没有指定,jQuery将智能地尝试获取结果,基于响应的MIME类型(XML MIME类型将产生XML,在1.4 JSON中将产生一个JavaScript对象,在1.4脚本中将执行脚本,以及任何东西else将作为字符串返回)。

  2. I don't think that jQuery can successfully transform your data to a query string. 我不认为jQuery可以成功地将您的数据转换为查询字符串。 You are trying to send an array of objects: 您正在尝试发送一组对象:

     {"id_consulta":[{"id":1},{"id":2},{"id":3}]} 

    Use Firebug and examine which data is actually sent. 使用Firebug并检查实际发送的数据。 If you want to send the whole string as JSON, you have to set the processData option to false: 如果要将整个字符串作为JSON发送,则必须将processData选项设置为false:

     $.ajax({ type: "POST", url: "getUbicaciones.php", data: "json=" + $.toJSON(mJSON.id_consulta), processData: false, //.... 

    and you have to decode the string on the server side: 你必须解码服务器端的字符串:

     $data = json_decode($_POST['json']); 

Finally made it work!. 终于成功了! It went like this: 它是这样的:

JavaScript: JavaScript的:

var sJSON = $.toJSON(mJSON.id_consulta);


            $.ajax({
                type: "POST",    
                url: "getUbicaciones.php",  
                data: "json=" + sJSON,                  
                processData: false,             
                success: function(respuesta){       

                },
                error: function (request,error){

                }
            }); 

PHP: PHP:

$m_decoded = json_decode(stripslashes($_POST["json"])); 

Note that I had to use "stripslashes" since the JSON string had slashes for the " character. 请注意,我必须使用“stripslashes”,因为JSON字符串具有“字符”的斜杠。

Thank you everyone for all your help, I hope this helps someone else. 谢谢大家的帮助,我希望这有助于其他人。

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

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