前后端数据交换的相关方法

分享 未结
0 2908
番茄
番茄 VIP 1 2018-05-18
悬赏:0

常用相关数据格式

jq获取表单数据集合

  • serialize()
  • serializeArray()
  • param()
  1. <script type="text/javascript" src="js/jquery-3.2.1.js" ></script>
  2. <script>
  3. $(function(){
  4. $("#btn").click(function(){
  5. //console.log($("#form1").serializeArray());
  6. console.log($(":text,select,:checked").serializeArray());
  7. //console.log("hello world");
  8. })
  9. })
  10. </script>

js 对象和json字符串转换

  • JSON.stringify()
  • JSON.parse()


php json编码/解码

  • json_encode()
  • json_decode()


php 序列化/反序列化

  • serialize()

  • unserialize()


functions.php相关自定义函数

  • json_file_put() 保存为json文件
  • json_file_get() 从json文件取出json字符串或php数组
  • array_file_put() 将数组保存到文件
  • array_file_get() 从文件读取数组
  • fieldIndex() 以指定字段为索引返回数据库所取的数据数组,与js的hasOwnProperty()配合使用
  • V() 根据文件最后修改时间输出带版本号src,用于强制更新缓存
  1. </script>
  2. var jsonInfo = <{:json_encode($info,JSON_UNESCAPED_UNICODE)}>;
  3. var jsonValue = <{:json_file_get("/base_json/curd.json")}>;
  4. </script>
  5. <script charset="UTF-8" src="<{:V('/layui/layui.js')}>"></script>
<?php
$arr = json_file_get("/base_json/curd.json",true);



/**
 * 保存为json文件
 * @param [type] $arr       json的数据源
 * @param [type] $filePath  文件路径
 * @param string $location  保存位置
 */
function json_file_put($arr,$filePath,$location='pub'){
    $location = strtolower($location);
    switch ($location) {
        case 'res':
            $dir = RES;
            break;
        case 'pub':
        default:
            $dir = PUB;
            break;
    }
    $filePath = '/'.ltrim($filePath,'/');
    $json = json_encode($arr,JSON_UNESCAPED_UNICODE);
    return file_put_contents($dir.$filePath, $json);
} //保存为json文件


/**
 * 从json文件取出数据
 * @param string $filePath     文件路径
 * @param string $jsonDecode   是否解码
 * @param string $location     保存位置
 * @return mixed 解码为真返回php数组,为假返回json字符串
 */
function json_file_get($filePath,$jsonDecode=false,$location='pub'){
    $location = strtolower($location);
    switch ($location) {
        case 'res':
            $dir = RES;
            break;
        case 'pub':
        default:
            $dir = PUB;
            break;
    }
    $filePath = '/'.ltrim($filePath,'/');
    $content = file_get_contents($dir.$filePath);
    if($jsonDecode===true){
        $content = json_decode($content,true);
    };
    return $content;
} //从json文件取出数据




回帖

温馨通道

最近回贴

最近热议
没有相关数据