【问题标题】:Best practice for sending JSON to php with keeping field types将 JSON 发送到 php 并保留字段类型的最佳实践
【发布时间】:2022-01-17 11:38:25
【问题描述】:

显然,如果我使用 AJAX 发布或获取请求将 JSON 数据发送到 php,我最终将只接收 php $_POST 变量中的字符串数据类型。

发送 JSON 字段但保留其数据类型的最佳做法是什么?我的想法是在一个 $_POST 字段中发送整个 JSON,然后再解码一次。

所以在 JavaScript 中而不是:

$.post('test.php', {"stringparm":"a string","numparm": 66,"boolparm":true)}, function (result) ... 

我会使用:

params = '{"stringparm":"a string","numparm": 66,"boolparm":true)}';
$.post('test.php', {'params': params}, function (result) { 
....
}

在 php 中我会:

$params = json.decode($_POST['params']);

或者有更好的方法吗?

【问题讨论】:

    标签: javascript php json types


    【解决方案1】:

    我的建议是使用 Fetch 或 Axios 将它们作为查询字符串发送,您也可以使用 Ajax,它应该正确发送各种数据类型,然后像这样在 PHP 中解析查询字符串。

    parse_str($_SERVER['QUERY_STRING'], $data);
    $data = (object) $data;
    

    我确定你知道你的标准查询字符串是什么样子的

    https://example.com/?user_id=value&someotherdata=value etc...
    

    一旦您拥有 $data 对象,您就可以使用箭头 -> 操作符访问这些值。

    $user_id = $data->user_id
    

    这是我用来从提供的对象创建查询字符串的脚本。

      prepareQuerystring(url, paramsObject, isParamsObjectUsed, phpMethod = '') {
    if (url !== '') {
      let postUrl = `${url}${phpMethod}`;
      if (isParamsObjectUsed) {
        postUrl += `?`;
        let it = 0;
        let len = Object.keys(paramsObject).length
        for (let i in paramsObject) {
          if (it === len - 1) {
            postUrl = postUrl + [i] + '=' + paramsObject[i];
          } else {
            postUrl = postUrl + [i] + '=' + paramsObject[i] + '&';
          }
          it++;
        }
        return postUrl;
      } else {
        postUrl = `${url}${phpMethod}/`;
        return postUrl;
      }
    } else {
      let postUrl = `${window.location.href}${phpMethod}/`;
      if (isParamsObjectUsed) {
        postUrl += `?`;
        let it = 0;
        let len = Object.keys(paramsObject).length
        for (let i in paramsObject) {
          if (it === len - 1) {
            postUrl = postUrl + [i] + '=' + paramsObject[i];
          } else {
            postUrl = postUrl + [i] + '=' + paramsObject[i] + '&';
          }
          it++;
        }
        return postUrl;
      } else {
        postUrl = `${window.location.href}${phpMethod}/`;
        return postUrl;
      }
    }
    

    }

    url 参数是你的基本 url (https://example.com)

    paramObject 是您的 JSON 对象,它只是 Javascript 中的一个对象。

      {
        user_id: 1,
        someotherdata: 'shoe'
      }
    

    PHP 方法参数用于调用 php 中的特定方法,它只是附加在基本 url 之后,我的 API 允许我以这种方式进行调用,您的可能不会,所以您可以将其保留为空字符串 '' .

    isParamsObjectUsed 是一个布尔值,如果你在第二个参数中传递一个对象,那么这将是真的,老实说,我不记得曾经将它设置为 false 所以可能需要重构并最终删除它。

    这是一个如何使用它的示例。

          let url = prepareQuerystring(
            https://example.com/,
            {
              user_id: 1,
              someotherdata: 'shoe',
              someBool: true
            },
            true
          )
    

    使用此方法,您将在 PHP 中引用所有参数作为它们的数据类型。我使用 Axios 发送 http 请求,但这也适用于 Ajax 和 Fetch,我建议使用 Fetch 或 Axios。主要是因为我不记得我是如何使用 Ajax 的,所以我通常使用 Fetch 或 Axios。

    Using Fetch

    fetch(url, { method: "POST" })
    

    希望这有助于为您指明正确的方向。如果您有任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-26
      • 2011-06-01
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      相关资源
      最近更新 更多