javascript - 如何在本地文件夹上调用json

标签 javascript php jquery html json

如何使用 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>

控制台上显示错误

enter image description here

这是我的 json 代码:

{
    "user": {
        "id": 21821290968,
        "url": "https://51talk.zendesk.com/api/v2/users/21821290968.json",
        "name": "Rovi Roy Cruz",
        "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="11637e67783f7263646b51242065707d7a3f727e7c" rel="noreferrer noopener nofollow">[email protected]</a>",
        "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"
        }
    }
}

最佳答案

检查下面的代码,它适合您:

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

关于javascript - 如何在本地文件夹上调用json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44711872/

相关文章:

PHP fopen() 不创建文件

PHP fatal error : Call to a member function bind_param() on boolean

JQuery 多个复选框组验证

php - HTML 页面中的 JSON 数据在表中

javascript - 包含 iframe 的页面上的 Bootstrap 3 导航栏

javascript - 使用 qtip 的动态 javascript 数据覆盖所有具有相同消息的工具提示

javascript - 如何使用 jQuery 循环访问 WordPress WP_Query 对象?

jquery - ASP.NET MVC 中的 AJAX 表单提交模式

javascript - 在预加载器文本中添加链接

javascript - 如果所有选择都有值,则 jQuery 显示按钮