美文网首页WebWeb前端之路Web 前端开发
改变从后台接口请求到的数据的格式

改变从后台接口请求到的数据的格式

作者: 柠檬树QAQ | 来源:发表于2017-03-03 03:48 被阅读195次

注释都写到行间了 很详细


    <script src="js/jquery.js"></script>
    <script src="js/angular.js"></script>
    <script>
        let app=angular.module('app',[]);
        //let n=0;
        app.controller('test',function ($scope,$http) {
            $http.get('/get').success(function (data) {//请求数据
                /*
                * $scope.Periphery=[];--用来存放周边和热点地区的 值
                 $scope.Popular=[];
                * */
                $scope.arr=[];
                $scope.json={};
                $scope.Periphery=[];
                $scope.Popular=[];
                if(data.error){
                    $scope.data=data.data;
                    angular.forEach($scope.data,function (v, k) {//周边热门
                        $scope.json[v.Initials]=0;//json添加模式;把数据库里面的Initials 都放到json里面  给他赋值 每个数组的值  都是0
                        if(v.Periphery){//v.Periphery --的值是1或0 也就是 true/false 当结果为真的时候 就把结果放到 $scope.Periphery 数组里面
                            $scope.Periphery.push(v.area);
                        }
                        if(v.Popular){
                            $scope.Popular.push(v.area);
                        }

                    });

                    angular.forEach($scope.json,function (v1, k1) {
                        let arr2=[];
                        /*
                        * 循环json 定义一个空的数组用来放值 json有自动去重的属性 上面又给他所有的值都  赋值为0
                        * 例:s:0, a:0 ,......他的值是0  键s,...
                        * 得到  键 并且  去重
                        * */


                        angular.forEach($scope.data,function (v, k) {
                            if(k1==v.Initials){
                                arr2.push(v.area);
                            }
                            /*
                            * k1 是$scope.json的键 v.Initials 是数据库的某一列
                            * 在数据库的 某一列 里面 查找 k1 如果找到的话 就把对应的内容放到 上面定义$scope.arr2的数组里面
                            *
                            * */

                        });
                         $scope.json={};
                         $scope.json.a=k1;
                         $scope.json.b=arr2;
                         $scope.arr.push($scope.json);
                         /*
                         * 1、清空json数组里面的内容 让他都为空
                         * 2、重新给json赋值 把json的键k1 赋值给a,把arr2赋值给b-arr2是值
                         * 3、相当于 一个全新的json 摆在你的面前 a(k1)是-键--;b(arr2)--是值;
                         * 4、给全新的json放到--最先定义的数组--arr里面
                         * 5、格式:=== $scope.arr=[{a:'s',b:...},{a:'s',b:...},{a:'s',b:...},{a:'s',b:...}]
                         * */
                    });

                }else{
                    alert('失败')
                }
            });
            $http.get('/src').success((data)=>{
                if(data.error){
                    $scope.data=data.data;
                }else{
                    console.log('出错了')
                }
            });
            $scope.show=function (n) {
                $scope.selected=n;
            };
/*            $http.get('/gengd').success((data)=>{
                $scope.arr3=[];
                $scope.js2={};
                if(data.error){
                    $scope.data=data.data;
                    angular.forEach($scope.data,function (v, k) {
                        $scope.js2[v.inial]=0;
                    });
                    angular.forEach($scope.js2,function (v1, k1) {
                        let arr4=[];
                        angular.forEach($scope.data,function (v, k) {
                            if(k1==v.inial){
                                arr4.push(v.name);
                            }
                        });
                        $scope.js2={};
                        $scope.js2.a=k1;
                        $scope.js2.b=arr4;
                        $scope.arr3.push($scope.js2)
                    });


                }else{
                    alert(1)
                }

            })*/
        })
    </script>

Node.js 接口

const express=require('express');
const static=require('express-static');
const mysql=require('mysql');

let server=express();
server.listen(4100);
let db=mysql.createConnection({host:'localhost',user:'root',password:'',database:'mysql'});

server.get('/get',(req,res)=>{
    "use strict";
    db.query('SELECT * FROM first',(err,data)=>{
        if(err){
            res.send({error:false,msg:'出错了'})
        }else{
            res.send({error:true,data:data})
        }
        res.end();
    })
});
server .get('/src',(req , res)=>{
    "use strict";
    db.query('SELECT * FROM SRC',(err,data)=>{
        if(err){
            res.send({error:false,msg:'出错了'})
        }else{
            res.send({error:true,data:data})
        }
        res.end();
    })
});
server.get('/gengd',(req,res)=>{
    "use strict";
   db.query('SELECT * FROM gengd',(err,data)=>{
       if(err){
           res.send({error:false,msg:'数据库加载失败'})
       }else{
           res.send({error:true,data:data})
       }
       res.end();
   })
});
server.use(static('www'));




相关文章

网友评论

    本文标题:改变从后台接口请求到的数据的格式

    本文链接:https://www.haomeiwen.com/subject/oxctgttx.html