假设我有一个“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/