PDA

View Full Version : button submit - display response message from json



qt4x11
8 Feb 2012, 1:55 PM
My code submits a few params to a remote servlet. The servlet returns a JSON response - I can see that my request succeeded on the server, but in my js console I'm seeing


Uncaught Ext.Error: You're trying to decode an invalid JSON String: <?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?><Response>


It seems I'm not able to deal with the JSON returned by the server? I haven't found any solutions yet that will allow me to get past this error - any suggestions?



Ext.define('Ext.reports.CallbackReportEmailButton', {
extend : 'Ext.form.Panel',
alias : 'widget.reports-callbackReportEmailButton',
border : 0,
buttonAlign : 'center',
buttons : [{
text : 'Send Email',


// disabled : true,


handler : function() {


var emailTo = document.getElementById('emailTo').value;
var emailSubj = document.getElementById('emailSubj').value;
var emailBody = document.getElementById('emailBody').value;
var linkUrl = document.getElementById('linkUrl').value;
var linkCallbackID = document.getElementById('linkCallbackID').value;
alert(emailTo);
alert(linkUrl);

this.up('form').getForm().submit({
url : '/enterprise/SendEmailInvite',
params: { emailTo: emailTo,emailSubj:emailSubj,emailBody: emailBody,linkUrl:linkUrl,linkCallbackID: linkCallbackID },
method : 'POST',
submitEmptyText : false,
waitMsg : 'Sending ...',
success : function(response, opts) {
alert("successfull");
},
failure : function(res, opt) {
alert("request failed");
}
})
},


itemId : 'callbackReportEmailButton'


}]
});

sskow200
8 Feb 2012, 2:15 PM
Just a question...why are you redundantly setting the field values as params in the form?




params: { emailTo: emailTo,emailSubj:emailSubj,emailBody: emailBody,linkUrl:linkUrl,linkCallbackID: linkCallbackID }



It is my understanding that the form will automatically post the values you're looking for? As far as the response, I would check to make sure your url is appropriate and you also may need to think about using the standardSubmit configuration.


standardSubmit: true

qt4x11
8 Feb 2012, 2:18 PM
I have to set the field vals as params in the form due to how the form is written - populated from grid row selection into a template in another panel, so this is how I get these values into this button. I know the URL is working because I can see in the server logs that the request is succeeding - extjs is just not able to deal with the json response message.

qt4x11
8 Feb 2012, 2:26 PM
Why would I have to change how I'm submittig the data? It looks like the submit is working? It's just the response that is causing problems?

sskow200
8 Feb 2012, 2:29 PM
The response looks to be XML...it's kind of difficult to give you support in this issue because I can't replicate your environment. Also, if it's just an ajax request, why create a form in general? If you're populating the values from a grid, seems like it would just be simpler to manually submit an Ext.Ajax.request.

qt4x11
8 Feb 2012, 2:32 PM
That sounds worth a try - I still need a button as a way to trigger submission of this ajax request. Can I do an Ext.ajax.request in the handler of the button?

sskow200
8 Feb 2012, 2:39 PM
Ext.create('Ext.Button', {
text: 'Send E-Mail',
handler: function(){
Ext.Ajax.request({
url : '/enterprise/SendEmailInvite',
params: {
emailTo: emailTo,
emailSubj:emailSubj,
emailBody:emailBody,
linkUrl:linkUrl,
linkCallbackID: linkCallbackID
},
method : 'POST',
success : function(response, opts) { alert("successfull"); },
failure : function(res, opt){ alert("request failed");}
});
});



This will save you a DOM read/write as you don't need to create a form element just to submit the data.

qt4x11
8 Feb 2012, 2:46 PM
Thank you! I tried but I still get the same error -


Uncaught Ext.Error: You're trying to decode an invalid JSON String: <?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?><Response>Email Sent Successfully</Response>

at this point I think the xml being returned by the server is the problem? as in, Ext is not able to read it due to the way the response is formatted.

sskow200
8 Feb 2012, 3:01 PM
Correct. First you need to investigate what the payload looks like to the client. Also, you may need to adjust the headers in the request/response to modify the Content-type: 'application/json' or Content-type: 'text/xml' depending on what you'd prefer.