4
我想用ExtJS 4.1创建一个网格面板。它使用AJAX代理从服务器获取其数据:ExtJS Ajax POST与代理POST
var store = Ext.create('Ext.data.Store', {
model: 'myModel',
pageSize: pageSize,
proxy: {
type: 'ajax',
url: "../search",
actionMethods: {
create: "POST",
read: "POST",
update: "POST",
destroy: "POST"
},
headers: {
'Content-Type': 'application/json'
},
limitParam: false,
startParam: false,
pageParam: false,
extraParams: JSON.stringify({
rows: pageSize,
role: "Admin",
index: myIndex,
question: searchPhrase
}),
reader: {
type: 'json',
root: 'results.results',
totalProperty: 'numFound',
model: 'myModel'
}
}
});
store.loadPage(1);
但它似乎不工作。
我收到一条错误消息,指出无法读取JSON。更重要的是,在Firebug中,发送的参数不是人类可读的。
当我尝试做一个Ajax调用使用相同的参数,似乎一切都OK:
Ext.Ajax.request({
url:"../search",
method: "POST",
params: JSON.stringify({
rows: pageSize,
role: "Admin",
index: myIndex,
question: searchPhrase
}),
success: function(){
console.log("ok");
},
failure: function(response, opts){
console.log("failed");
},
headers: {
'Content-Type': 'application/json'
}
});
即使在Firebug,在请求每个参数看起来只有精细。
使用代理时,框架有什么不同?
有可能与你的第一个代码进行检查的几件事情:1)我会尝试从'extraParams'删除'JSON.stringify' - 它只适用于发送对象。 2)为了安全起见,尽量包含代理配置'writer:'json''。 3)评论'headers'配置。 – Izhaki 2012-08-07 13:59:25
服务器响应也可能存在问题,因此如果您还包含服务器返回的JSON,将会有所帮助。 – Izhaki 2012-08-07 13:59:55
@Izhaki如果我删除了'JSON.stringify',那么我得到另一个错误(我发现这种方法避免它在互联网上的某个地方)。如果我对'headers'配置发表评论,那么错误的'content-type'正在设置(同样是一个问题)。关于答复,我不认为存在问题,因为我得到了302状态,错误('无法读取JSON')来自服务器。还有其他建议吗? – Dragos 2012-08-07 14:27:42