【问题标题】:PHP can't retrieve JSON from ajax callPHP 无法从 ajax 调用中检索 JSON
【发布时间】:2016-11-17 13:11:16
【问题描述】:

这是我的 JS 代码:

var person = [];
    person[0] = "John";
    person[1] = "Doe";
    person[2] = 46;
    var myData = JSON.stringify(person);
    $.ajax({
        type: "POST",   
        url: "test.php",
        dataType : "text", 
        contentType: "application/json; charset=utf-8",
        data: myData,
        success: function(answer) {
            alert(answer);
        },
        complete: function() {
        },
        error: function(jqXHR, errorText, errorThrown) {
            alert(jqXHR+" - "+errorText+" - "+errorThrown);
        }
    });

这里是 php:

if(isset($_POST['myData']))
{
 echo "ok";
}
else
{
 echo "not_ok";
}
?>

而且它总是返回“not_ok”。为什么我的 PHP 代码无法检索 JSON?我做错了什么?谁能给我解释一下?

【问题讨论】:

  • 你应该使用json_encode();
  • data: {myData : myData},

标签: jquery json ajax post


【解决方案1】:

下面试试

dataType : "json":告诉 jQuery 你希望它解析返回的 JSON

json_encode() : PHP 函数将数组编码为 json 格式。

JavaScript

var person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46;

$.ajax({
    type: "POST",   
    url: "test.php",
    dataType : "json",  // Set datatype json
    data: {myData : person}, // Request Parameters
    success: function(answer) {
        console.log(answer); // JSON Response
    },
    complete: function() {
    },
    error: function(jqXHR, errorText, errorThrown) {
        alert(jqXHR+" - "+errorText+" - "+errorThrown);
    }
});

PHP

<?php
    if(isset($_POST['myData'])){
        $status = "ok";
    }
    else
    {
        $status = "Not Ok";
    }
    echo json_encode(array("status" => $status));
    exit;
?>

输出

Object {status: "ok"}

【讨论】:

  • 又是我。我错了....它返回“不好”。不知道为什么,我复制并粘贴了你的代码而不是我的。
  • 请在if(isset($_POST['myData'])){ 之前写print_r($_POST); 并显示输出
  • 数组 ( ) {"status":"Not Ok"}
  • 请将data: {'myData' : person}, 替换为data: {myData : person}, 并告诉我们输出
  • 很奇怪。更改后它起作用:“对象{状态:“确定”}”。直到我添加“print_r($_POST);”在 php 中,因为该警报发生后:“[object Object] - parsererror - SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data”
【解决方案2】:

您需要一些键来从 post/get 变量中获取数据

 data:{firstname:"John",lastname:"Doe",age:45},

php:

$_POST['firstname']

data:{data:["John","Doe",45]},

如果你真的想要一个数组

 $_POST['data']

【讨论】:

    【解决方案3】:

    您正在将数组转换为没有任何名称的 json 格式的字符串。但是您可以在 ajax 帖子中发布数组,您不需要对其进行字符串化。 你可以按原样传递你的数组

    data: {myData: person},
    
    
    
    var person = [];
        person[0] = "John";
        person[1] = "Doe";
        person[2] = 46;
    
    $.ajax({
        type: "POST",   
        url: "test.php",
        dataType : "text", 
        contentType: "application/json; charset=utf-8",
        data: {myData: person},
        success: function(answer) {
            alert(answer);
        },
        complete: function() {
        },
        error: function(jqXHR, errorText, errorThrown) {
            alert(jqXHR+" - "+errorText+" - "+errorThrown);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-15
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      • 2018-01-23
      • 2013-07-02
      相关资源
      最近更新 更多