我正在使用yii框架在Ext Js4中开发网站。我遇到了一个问题,即如何访问从服务器返回到外部js 4的json数据。实际上我想显示消息字段以及每个字段在外部js的json代码中4.我该怎么做?
这是我的json代码(json形式的服务器响应)
{
"success": true,
"data": [
{
"msg": "uname and pass wrong"
}
]
}
这是我的模型类代码
Ext.define('AM.model.Login',
{
extend:'Ext.data.Model',
idProperty:'id',
fields:['id','uname','pass'],
proxy:
{
type:'ajax',
url:'http://localhost/Balaee3/index.php?r=site/contact',
reader:
{
type:'json',
root:'data',
successProperty:'success'
},//end of reader
writer:
{
type:'json',
root:'records',
//writeAllFields:true
},//End of writer
}//end of proxy
}
);
如何在ext js4 中访问控制器文件中的json代码 在这里,我想显示我的json数据。
AM.model.Login.load(512, {
params: {
id: 512
},
success: function(record, operation) {
character = record;
},//end of success
failure: function(record, operation)
{
Ext.Msg.alert('Error', 'Failed to load model from the server.');
},//end of failure
callback: function(record, operation) {
//Ext.Msg.alert('Success', 'Model loaded successfully.');
console.log('Success', 'Model loaded successfully.');
}//end of call back function
});
给我一些建议
答案 0 :(得分:2)
在模型中添加messageProperty。 尝试这样的事情....
reader: {
type: 'json',
root: 'data',
successProperty : 'success',
messageProperty : 'message',
implicitIncludes: true
}
def message=''
def json = request.JSON
message=data.message
def metaData = [ successProperty : 'success', messageProperty : 'message', totalProperty:'num', root:'data']
def jsonData = [metaData:metaData, success:flag, message:message]
render jsonData as JSON