duanliyi5997 2015-05-02 17:46
浏览 96
已采纳

将参数传递给ajax调用以只读取json文件的特定字段

I have a example.json file like this:

{
"User1": [{
           "Age":21,
           "Dogs":5,
           "Cats":0
          }],
"User2": [{
           "Age":19,
           "Dogs":2,
           "Cats":1
         }]
"User3": [ ...and so on...]
}

and a function LoadData(UserName); like this:

    LoadData(UserName) {
       $.ajax({
           url: "/users/example.json",
           dataType: "json",
           data: // How can i say to AJAX to parse only the UserName array? 
           success: function(response) {
                var data = $.parseJSON(response);
                $.("#ageID").text(response.Age);
                $.("#dogsID").text(response.Dogs);
                // And so on with the others fields
           }
       });
}

Also.. the function LoadData(UserName); is called by the main html file (actually a .php script) with something like this:

<head>
      <script type="text/javascript">

          $(document).ready(function() {

                LoadData("<?php echo $_SESSION['user_name']; ?>");
          });

      </script>
</head>

Thanks!

NOTE: i perfectly know that all data i parse/use in this way will be perfectly visible to anyone. The example above is just, indeed, an example. The data i am dealing with is not private, rather is actually public.

  • 写回答

2条回答 默认 最新

  • douxiajia6309 2015-05-02 17:48
    关注

    You can't. The server decides how it responds. You'll just have to deal with the returned data and ignore anything you don't need.

    If you are also developing the server side application, then you can modify it to detect for example query parameters and react to those. Or use a different URL for getting only the UserName array.

    I'm quessing here, but let's say you want to load all the user data only once but want to have a function that only deals with a single user at a time, try something like this:

    function loadUsers() {
       return $.ajax({
           url: "/users/example.json",
           dataType: "json"
       });
    }
    
    function renderUser(user) {
        $("#ageID").text(user.Age);
        $("#dogsID").text(user.Dogs);
        // And so on with the others fields
    }
    
    function loadAndRender() {
        loadUsers().done(function(users) {
            renderUser(users.User1);
            renderUser(users.User2);
            // or loop over all of them, or have the required names as arguments in loadAndRender, etc
        });
    }
    

    Some additional notes and unsolicited advice:

    • no need to call parseJSON in the success function (or the done callback in my example)

    • in JS, only capitalize functions that are to be used as constructors. Don't capitalize anything else.

    • $.("#ageID") should be $("#ageID") (or in my personal preference $('#age'))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程