PDA

View Full Version : Multipart POST never hits success handler, though success if in response.



xjscrafter
5 Mar 2014, 4:49 PM
I have a multipart POST file upload, and even though I have success: true in the response, the success handler never gets called. The failure handler is always called.

If I set a breakpoint in the failure handler and look at the action object, I see it has this for the response responseText:

{success:false,message:"Permission denied to access property 'document'"}

So although the response I see in firebug has success: true, the action object has success as false.

Any ideas?


}, {
xtype: 'filefield',
width: 450,
height: 30,
name: 'licensefile',
allowBlank: false,
itemId: 'licensePath',
maxHeight: 30,
buttonMargin: 10
}


uploadLicense: function() {
var form = this.getUploadLicenseForm();
if(form.isValid()){
form.submit({
method: 'POST',
url: CommonUtil.globalRestWSURL + '/amc/license/upload',
headers : {'Content-Type':'multipart/form-data'},
waitMsg: 'Uploading license...',
submitEmptyText: false,
scope: this,
success: function(form, action) {
action.scope.getSettinglicense().close();
action.scope.getLicenseStatusMessage().setMessage('License upload succeeded.');
action.scope.getLicenseStatusMessage().setMode('success');
action.scope.getLicenseStatusMessage().setVisible(true);
action.scope.refreshLicenseGrid();
},
failure: function(form, action){
action.scope.getSettinglicense().close();
action.scope.getLicenseStatusMessage().setMessage('License upload failed.');
action.scope.getLicenseStatusMessage().setMode('error');
action.scope.getLicenseStatusMessage().setVisible(true);
// remove this when success handler is workingproperly, but now always
// failure handler, never get success handler
action.scope.refreshLicenseGrid();
}
}, this);
}
}

evant
5 Mar 2014, 4:53 PM
Make sure you've taken care of this: http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.form.Basic-method-hasUpload

xjscrafter
5 Mar 2014, 5:05 PM
It looks like hasUpload is a getter. What would I do with it in this case?

evant
5 Mar 2014, 5:08 PM
Read it!



Returns true if the form contains a file upload field. This is used to determine the method for submitting the form: File uploads are not performed using normal 'Ajax' techniques, that is they are not performed using XMLHttpRequests. Instead a hidden <form> element containing all the fields is created temporarily and submitted with its target set to refer to a dynamically generated, hidden <iframe> which is inserted into the document but removed after the return data has been gathered.

The server response is parsed by the browser to create the document for the IFRAME. If the server is using JSON to send the return object, then the Content-Type header must be set to "text/html" in order to tell the browser to insert the text unchanged into the document body.

Characters which are significant to an HTML parser must be sent as HTML entities, so encode "<" as "&lt;", "&" as "&amp;" etc.

The response text is retrieved from the document, and a fake XMLHttpRequest object is created containing a responseText property in order to conform to the requirements of event handlers and callbacks.

Be aware that file upload packets are sent with the content type multipart/form and some server technologies (notably JEE) may require some custom processing in order to retrieve parameter names and parameter values from the packet content.

xjscrafter
5 Mar 2014, 5:12 PM
Yes, but the doc only says what the function does. Where do I call this function so my success handler ends up being called? Do I call it somewhere in this code as the call is made?


form.submit({
method: 'POST',
url: CommonUtil.globalRestWSURL + '/amc/license/upload',
headers : {'Content-Type':'multipart/form-data'},
waitMsg: 'Uploading license...',
submitEmptyText: false,
scope: this,
success: function(form, action) {
action.scope.getSettinglicense().close();
action.scope.getLicenseStatusMessage().setMessage('License upload succeeded.');
action.scope.getLicenseStatusMessage().setMode('success');
action.scope.getLicenseStatusMessage().setVisible(true);
action.scope.refreshLicenseGrid();
},
failure: function(form, action){
action.scope.getSettinglicense().close();
action.scope.getLicenseStatusMessage().setMessage('License upload failed.');
action.scope.getLicenseStatusMessage().setMode('error');
action.scope.getLicenseStatusMessage().setVisible(true);
// remove this when success handler is workingproperly, but now always
// failure handler, never get success handler
action.scope.refreshLicenseGrid();
}
}, this);

xjscrafter
5 Mar 2014, 5:36 PM
Maybe I need to do this on the server side:

If the server is using JSON to send the return object, then the Content-Type header must be set to "text/html" in order to tell the browser to insert the text unchanged into the document body.

salilsawant
12 Nov 2014, 12:07 AM
Do you the page URL differ with the form URL?

If so then it could be CORS issue.

Please check this: https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

xjscrafter
12 Nov 2014, 8:39 AM
That's not it, but thanks for trying.