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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配