【问题标题】:ajax post returning string instead of json objectajax 发布返回字符串而不是 json 对象
【发布时间】:2017-08-17 05:27:19
【问题描述】:

我的代码在 PHP 中返回 dataType 对象,但是当我使用 AJAX 调用相同的函数时,它会将数据类型作为字符串返回给我。我希望数据类型是 JSON 对象。

PHP 代码

$result = $manualRequest->getUser($_POST['phonenumber']);

print_r($result);

这实际上是一个已解析的数据库对象

AJAX 代码

function getCustomer() {
        var callerNumber = $('#caller_number').val();
        var data = {
            'phonenumber': callerNumber
        };
        var url = "customerRequest.php";

    $.ajax({
        url: url,
        data: data,
        type: 'POST',
        dataType: 'JSON',
        success: function (result) {
            console.log(result);
        }
    });
}

我得到了想要的结果,但我想要的是 JSON 对象而不是字符串。

【问题讨论】:

  • $manualRequest->getUser($_POST['phonenumber']); 返回什么
  • print(json_encode($result))

标签: php jquery json ajax parse-platform


【解决方案1】:

如果你想要漂亮的 JSON

$result = $manualRequest->getUser($_POST['phonenumber']);
echo json_encode($result , JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT );

编辑

$.ajax({
  url: url,
  type: "POST",
  dataType: "json",
  data: data,
  success: function (response) {
    console.log(response);
  }
})
  .done(function () {
    console.log("success");
  })
  .fail(function () {
    console.log("error");
  })
  .always(function () {
    console.log("complete");
  });

【讨论】:

    【解决方案2】:

    在 javascript 中,您可以使用 JSON.parse 方法将字符串 JSON 解析为 JSON 对象。

    该方法的文档:https://www.w3schools.com/js/js_json_parse.asp

    【讨论】:

    • 我实际上是从 php 返回一个 PARSE(an MBASS) 对象,当我尝试你告诉我的内容时,我收到以下错误 Uncaught SyntaxError: Unexpected token P in JSON at position 0 at JSON.parse () 在 Object.success (startrequest.php:2012) 在 Function.handleSuccess (jquery-ui.min.js:142)
    • 哦,好的,我明白了。看来您的 PHP 没有将响应发送到 JSON 格式。我认为您可以使用 Željko Krnjić 的解决方案。您必须使用正确的格式设置响应标头: header('Content-type:application/json;charset=utf-8');并使用 json.encode 方法以格式化的 JSON 对象进行响应。
    【解决方案3】:

    在php文件中添加:

    header('Content-type:application/json;charset=utf-8');
    echo json_encode($result);
    

    而不是print_r($result);

    【讨论】:

    • 在您的 javascript 文件中,将行 console.log(result) 更改为 console.log(JSON.parse(result));
    • 这给了我以下错误 Uncaught SyntaxError: Unexpected token P in JSON at position 0 at JSON.parse () at Object.success (startrequest.php:2012) at Function.handleSuccess (jquery-ui.min.js:142)
    • 好的,使用console.log(result)(不带JSON.parse)并将结果粘贴给我。你确定它已经不是 JSON 了吗?
    • 我在做 console.log(typeof(result)) 它给了我字符串
    • 只需将console.log(result)的结果粘贴到这里,不带typeof...我想看看你得到的json或字符串是如何输出的...你也可以粘贴输出到 jsonlint.com 看看它是否是一个有效的 json..
    【解决方案4】:

    print_r 一般不会返回有效 JSON,你想做

    $result = $manualRequest->getUser($_POST['phonenumber']);
    echo json_encode( $result );
    

    只要它是有效的 JSON,并且 dataType 设置为 json,jQuery 就会对其进行解析,否则任何其他内容都会在您的 ajax 请求中导致“解析错误”。

    【讨论】:

      猜你喜欢
      • 2012-11-26
      • 1970-01-01
      • 2019-10-05
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多