duanliyi5997
duanliyi5997
2015-05-02 17:46

将参数传递给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 douxiajia6309 6年前

    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'))

    点赞 评论 复制链接分享
  • dongshijiao6890 dongshijiao6890 6年前

    Try This

    index.html

    <!DOCTYPE html>
    <html>
    <head>
    <title>Page Title</title>
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    </head>
    <body>
    <p id="ageID"></p>
    <p id="dogsID"></p>
    <script type="text/javascript">
    function LoadData(UserName) {
       $.ajax({
           url: "ajax.php",
           dataType: "json",
           data: {UserName:UserName},
           success: function(response) {
                $("#ageID").text(response[0].Age);
                $("#dogsID").text(response[0].Dogs);
           }
       });
    }
    LoadData('User2');
    </script>
    </body>
    </html>
    

    ajax.php page

    <?php
    $jsonfile = file_get_contents("example.json");
    $obj = json_decode($jsonfile);
    echo json_encode($obj->$_REQUEST['UserName']);
    ?>
    

    example.json

    {
    "User1": [{
               "Age":21,
               "Dogs":5,
               "Cats":0
              }],
    "User2": [{
               "Age":19,
               "Dogs":2,
               "Cats":1
             }]
    }
    
    点赞 评论 复制链接分享