ajax - 如何读取 json 文件?扩展程序

标签 ajax extjs model

假设我有一个“json”格式的用户字段:

{
    "users": [
       {
           "id": 1,
           "name": "Ed Spencer",
           "email": "ed@sencha.com"
       },
       {
           "id": 2,
           "name": "Abe Elias",
           "email": "abe@sencha.com"
       }
    ]
}

我假设这里的根是“用户”。我会将此文本保存为 users.json 文件吗?

从那里,我如何读取这个文件(在本例中,使用 ext.js)

Ext.define('AM.model.User', {
    extend: 'Ext.data.Model',
    fields: ['name', 'email']
});

var store = Ext.create('Ext.data.Store', {
    model: 'User',
    proxy: {
        type: 'ajax',
        url : 'users.json',
        reader: {
            type: 'json',
            root: 'users'
        }
    }
});

网址是什么?是绝对路径吗?这还能用吗???帮助!

最佳答案

在 HTML 页面中,JavaScript 文件是相对于 HTML 页面的 URL 或绝对路径提供的。

Would I save this text as a users.json file?

当然可以。

What is the url?

URL users.json 是相对于运行您的应用程序的 HTML 页面的 URL,因此如果 HTML 页面位于 http://localhost/myapp/index。 html,相对路径 users.json 将与绝对路径相同 http://localhost/myapp/users.json

Is it an absolute path?

不,users.json 是相对路径。如果愿意,您可以使用绝对路径。

我会在您的 app 文件夹中创建一个 data 文件夹,并使用 app/data/users.json 作为 URL。

您还应该学习 Chrome 开发者工具,以便您可以查看网络请求。

关于ajax - 如何读取 json 文件?扩展程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17819221/

相关文章:

eclipse - 为 Ext js 和 spket IDE 插件设置 eclipse

javascript - 可以在 extjs 中完成动态 Vtype 验证(如下代码)

javascript - JSONP- "Uncaught SyntaxError: Unexpected token"

javascript - 站点地图或 shebang 中的 escaped_fragment?

jquery - 如何将 AJAX 变量从 jQuery 传递到他们的 Controller ?

javascript - 想要设置投资组合项目/功能的父项

wpf - 如何在 Model-View-Viewmodel 架构中从模型方法的中间干净地获取用户输入?

Laravel 关系 一到两列

使用 CGridView、Yii 在 BELONGS_TO 模型列中搜索

javascript - 发布到 Node/快速端点的 Ajax 问题