PDA

View Full Version : [Solved] Problem with .NET web service and form save



mohaaron
18 Sep 2009, 3:44 PM
I'm trying to save a form and grid in one ajax request and keep having problems with posting the form and grid as json to the .net web service. It seems that the problem is the post is not being sent as json even though I'm using setting the header content type as json. I can see this in the Firebug headers tab. The one thing I do see that seems wrong to me is that the request header is different then the response header which I don't understand.

Why is the request header show the correct content type that I set "application/json; charset=utf-8;" and the response header does not "application/json". I don't know if this is the reason for my problem but I did notice this difference.

Here are the headers.


Response Headers
Server ASP.NET Development Server/8.0.0.0
Date Fri, 18 Sep 2009 23:25:23 GMT
X-AspNet-Version 2.0.50727
jsonerror true
Cache-Control private
Content-Type application/json
Content-Length 1066
Connection Close

Request Headers
Host localhost:2433
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
Connection keep-alive
Content-Type application/json; charset=utf-8;
X-Requested-With XMLHttpRequest
Referer http://localhost:2433/EquipmentManagerWebsite/Tests/EditorGrid.aspx
Here is the incorrect post and why is the first value undefined.


gridData=undefined%7B%22HardwareId%22%3A1%2C%22HardwareType%22%3A%22%22%2C%22Make%22%3A%22Dell%22%2C%22Model%22%3A%22Optiplex%20GX620%22%2C%22HardwareName%22%3A%22DTC1300274A6C20%22%7D%2C%7B%22HardwareId%22%3A2%2C%22HardwareType%22%3A%22%22%2C%22Make%22%3A%22Dell%22%2C%22Model%22%3A%22asdfasdf%22%2C%22HardwareName%22%3A%22sadfsadf%22%2C%22RetiredDate%22%3A%22%22%7D%2C%7B%22HardwareId%22%3A3%2C%22HardwareType%22%3A%22%22%2C%22Make%22%3A%22Dell%22%2C%22Model%22%3A%22Laptop%22%2C%22HardwareName%22%3A%22Computer%22%2C%22RetiredDate%22%3A%22%22%7D%2C%7B%22HardwareId%22%3A7%2C%22HardwareType%22%3A%22%22%2C%22Make%22%3A%22My%20Computer%22%2C%22Model%22%3A%22Comp%22%2C%22HardwareName%22%3A%22Compu%22%2C%22RetiredDate%22%3A%22%22%7D&HardwareType=asadfsadf&Make=sdfsadf&Model=asdfsdaf&HardwareName=asdfsadf&RetiredDate=asdfasdf
Here is my submit button handler. I got this code from link in the grid FAQ about how to send json grid data and form in one save action.


handler: function() {
var gridStore = grid.getStore();
var json;

gridStore.each(function(store) {
json += Ext.util.JSON.encode(store.data) + ',';
});
json = json.substring(0, json.length - 1);

gridForm.form.submit({
url: '../WebService.asmx/SaveHardware',
method: 'post',
headers: { 'Content-Type': 'application/json; charset=utf-8;' },
//jsonData: {gridData: json},
params: {
gridData: json
},
waitMsg: 'Saving Changes...',
success: function(form, action){
Ext.MessageBox.alert('Message', 'Save Successful.');
},
failure: function(form, action){
Ext.MessageBox.alert('Message', 'Save Failed');
}
});
}
Now I have also tried using Ext.Ajax.request which does work except now the jsonData value causes the form fields to be excluded from the post. So I now have only the grid data being posted. I also still have that undefined value in the grid data which I don't understand.


Ext.Ajax.request({
url: '../WebService.asmx/SaveHardware',
method: 'post',
headers: { 'Content-Type': 'application/json; charset=utf-8;' },
jsonData: {gridData: json}, // Using this fixes the json post problem but excludes the form fields from the post.
success: function(form, action){
Ext.MessageBox.alert('Message', 'Save Successful.');
},
failure: function(form, action){
Ext.MessageBox.alert('Message', 'Save Failed');
}
});
It seems like the jsonData config option is required to make this work but it causes my form to be excluded.

I can also see that once I include the jsonData config option I also get the correct content type of "application/json; charset=utf-8;" in the response header.


Response Headers
Server ASP.NET Development Server/8.0.0.0
Date Fri, 18 Sep 2009 23:41:16 GMT
X-AspNet-Version 2.0.50727
Cache-Control private, max-age=0
Content-Type application/json; charset=utf-8
Content-Length 55
Connection Close
Can anyone point me in the right direction?

mohaaron
21 Sep 2009, 11:05 AM
Just in case anyone else has this happen, the problem was me and not the code. The form fields are being passed in the Ajax.request.

I will say that I do wish there was a way to pass headers the grid.form.submit method so I could use it instead of being forced to use the Ajax.request which works with the headers config.