我的代码有问题。我正在尝试使用 ExtJS 和 Codeigniter 制作上传文件形式。
下面是我的代码,
Ext.require([
'Ext.form.field.File',
'Ext.form.Panel',
'Ext.window.MessageBox'
]);
Ext.onReady(function(){
Ext.create('Ext.form.Panel', {
title: 'Upload a Photo',
width: 400,
bodyPadding: 10,
frame: true,
renderTo: Ext.getBody(),
items: [{
xtype: 'filefield',
name: 'photo',
fieldLabel: 'Photo',
labelWidth: 50,
msgTarget: 'side',
allowBlank: false,
anchor: '100%',
buttonText: 'Select Photo...'
}],
buttons: [{
text: 'Upload',
handler: function() {
var form = this.up('form').getForm();
if(form.isValid()){
form.submit({
url: '../upload',
waitMsg: 'Uploading your photo...',
success: function(fp, o) {
Ext.Msg.alert('Success', 'Your photo "' + o.result.file + '" has been uploaded.');
Ext.Ajax.request({
url: o.data.url,
method: "GET",
success: function(response, request) {
// do whatever you need to with the generated HTML
alert(response.responseText);
},
failure: function(response, request) {
alert('failed');
}
});
}
});
}
}
}]
});
});
但我的控制台输出显示错误,如“Uncaught Ext.JSON.decode(): You're trying to decode an invalid JSON String”。所以,如果有人有同样的问题或正在解决这个问题,请告诉我..
谢谢!!!
结论: 哦,我明白了。实际上我使用 Codeigniter 作为我的框架。如果我将 json 返回到此 url,此问题将得到解决。
url: '../upload',
waitMsg: 'Uploading your photo...',
success: function(fp, o) {
Ext.Msg.alert('Success', 'Your photo "' + o.result.file + '" has been uploaded.');
Ext.Ajax.request({
url: o.data.url,
method: "GET",
success: function(response, request) {
// do whatever you need to with the generated HTML
alert(response.responseText);
},
failure: function(response, request) {
alert('failed');
}
});
所以,我像这样创建我的控制器(作为示例)..
public function upload(){
echo json_encode(1);
}
并且我的代码没有更多错误结果..谢谢Broncha!