【问题标题】:how to call json on local folder如何在本地文件夹上调用json
【发布时间】:2017-06-23 01:55:49
【问题描述】:

如何使用 javascript 和 php 调用本地 JSON 文件?... 我试过这个,但它有一个错误。我希望你能帮我解决这个问题

<html>
<head>
        <title></title>
        <script src="jquery.min.js"></script>
        <script src="jput-2.js"></script>
        <script src="jput-2.prod.js"></script>
        <script src="jput.min.js"></script>
        <script>
$(document).ready(function(){

var json = JSON.parse('ticket.json');
//while running this code the template will be appended in your div with json data
$("#tbody").jPut({
    jsonData:json,
    //ajax_url:"youfile.json",  if you want to call from a json file
    name:"tbody_template",
});

});
</script>   

    </head>
    <body>



<div jput="tbody_template">
 <tr>
  <td>{{ticket:id}}</td>
  <td>{{score}}</td>
 </tr>
</div>

<table>
 <tbody id="tbody">
 </tbody>
</table>


    </body>
</html>

控制台显示错误

这是我的 json 代码:

{
    "user": {
        "id": 21821290968,
        "url": "https://51talk.zendesk.com/api/v2/users/21821290968.json",
        "name": "Rovi Roy Cruz",
        "email": "rovi.cruz@51talk.com",
        "created_at": "2017-04-26T09:55:33Z",
        "updated_at": "2017-06-23T02:10:22Z",
        "time_zone": "Beijing",
        "phone": null,
        "shared_phone_number": null,
        "photo": null,
        "locale_id": 1,
        "locale": "en-US",
        "organization_id": null,
        "role": "admin",
        "verified": true,
        "external_id": null,
        "tags": [],
        "alias": "",
        "active": true,
        "shared": false,
        "shared_agent": false,
        "last_login_at": "2017-06-23T02:10:22Z",
        "two_factor_auth_enabled": null,
        "signature": "",
        "details": "",
        "notes": "",
        "custom_role_id": null,
        "moderator": true,
        "ticket_restriction": null,
        "only_private_comments": false,
        "restricted_agent": false,
        "suspended": false,
        "chat_only": false,
        "default_group_id": 42443967,
        "user_fields": {
            "system::embeddable_last_seen": "2017-06-21T00:00:00+00:00"
        }
    }
}

【问题讨论】:

    标签: javascript php jquery html json


    【解决方案1】:

    检查下面的代码它会为你工作:

    <script
      src="https://code.jquery.com/jquery-3.2.1.min.js"
      integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
      crossorigin="anonymous"></script>
     <script src="jput.js"></script>
    <script>
    $(document).ready(function(){   
    
    
            $("#users").jPut({
                ajax_url:"my.json",  //if you want to call from a json file
                name:"users",
            });
    
    });
    </script> 
    <div jput="users">
            <table border="1">
                <td>Id: {{id}}</td>
                <td>Id: {{email}}</td>
            </table>
    </div>
    <div id="users"></div>
    

    【讨论】:

    • 嗨@Harsh Patel,当然
    • 在控制台中检查你得到的错误,比如 cross origin.right??
    • 不,我的屏幕截图“未捕获的语法错误:位置 1 处的 JSON 中的意外标记 o”仍然出现相同的错误
    • @ivor 请检查我编辑的答案,它会为你工作
    • @ivor 检查我编辑的代码这对于将 json 转换为 html 很有用
    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多