PDA

View Full Version : IE problem forcing JSON



DigiDog
26 Jan 2010, 1:18 PM
Hi,

I'm pretty new to ExtJS, but getting the hang of it very quickly
My server-side is .Net, specifically asmx (web services).

My problem is that I am unable to configure forms and a JsonStore to actually use JSON to consume my web service (.Net web services only return JSON if request had Content-Type: application/json)

For example, to force my store to use JSON I do these 2 things:

1) Add a header in the HttpProxy

new Ext.data.JsonStore({
// store configs
proxy: new Ext.data.HttpProxy({
method: 'POST',
url: "json/common.asmx/getItems",
headers: {"Content-Type": "application/json; charset=utf-8"}

}),
2) On beforeload of the store, I copy parameters to the jsonData store parameter.


listeners:
{
beforeload: function(store, options)
{
Object.extend(store.proxy.conn.jsonData, options.params);
return true;
},

This generally allows me to get JSON from my webservice.
But now, after 2-3 weeks of development I realize that it doesn't work in IE.

Because in IE there's some extra content type header and my webservice fails to work.

in IE:

Content-Type: application/x-www-form-urlencoded; charset=UTF-8, application/json; charset=utf-8in FF:

Content-Type: application/json; charset=utf-8

Is there any way around this?
Is there a way to force ExtJS to use only application/json in the requests?
Should I be using some other technique to generate JSON on my .Net server-side?
Would switching to WCF help?

Thanks!

jsemmanuel
27 Jan 2010, 1:59 AM
I haven't used .NET but when php returns a json reponse, the headers are still text/html. It doesn't really matter what type the response is. Just that it is valid json. Isn't there some function like json_encode in php for .NET?

But as I said, I haven't used .NET before. Have a look at this though
http://stackoverflow.com/questions/288850/how-to-return-json-from-a-2-0-asmx-web-service

DigiDog
27 Jan 2010, 2:34 AM
But as I said, I haven't used .NET before. Have a look at this though
http://stackoverflow.com/questions/288850/how-to-return-json-from-a-2-0-asmx-web-service

It works when I use ajax.request, because I fully control the headers.
My problem is only with the extjs forms/stores, they add a second encoding header in IE.
I suppose it's a more generic problem with stores - because it uses the query to pass the parameters (like /service.asmx/myMethod?var1=1&var2=2

I would have expected the JsonStore to use JSON automatically when calling the server (I'm doing it manually and apparently, not good enough).

Thanks!

carol.ext
27 Jan 2010, 8:17 AM
Perhaps it would help if you edited the title of your initial post to include .NET and/or asmx so you get the attention of the .NET folks.

maxpower47
27 Jan 2010, 8:51 AM
I've been wrestling with this same issue all day and I think I finally got it figured out.

For me, I'm using the jquery adapter, so I was able to set the default contentType like this:


$.ajaxSetup({
contentType: 'application/json;charset=utf-8'
});Once I figured out what it was doing, I poked around in the regular Ext adapter source and it looks like you can do the same thing with this:


Ext.lib.Ajax.defaultPostHeader = 'application/json;charset=utf-8';

chessman
31 Jan 2010, 4:46 PM
I've been wrestling with this same issue all day and I think I finally got it figured out.

For me, I'm using the jquery adapter, so I was able to set the default contentType like this:


$.ajaxSetup({
contentType: 'application/json;charset=utf-8'
});Once I figured out what it was doing, I poked around in the regular Ext adapter source and it looks like you can do the same thing with this:


Ext.lib.Ajax.defaultPostHeader = 'application/json;charset=utf-8';

Have you solved it yet? I got the same problem with the header.

i've tried add
Ext.lib.Ajax.defaultPostHeader = 'application/json;charset=utf-8'; with in my code.
I run it but haven't display anything.
and I passed my parameter in store with
baseParams:{id:'abc'}

i checked "Fiddler" and it showed error "Invalid JSON primitive: id"

Do you have any ideas with that issue?

chessman
31 Jan 2010, 6:25 PM
Solved!!!! Finally i figured out what happened!!

the problem occurs when passing parameter..

if i get rid of baseParams and put
jsonData: Ext.util.JSON.encode({ id: termId }) in Httpproxy, then everything works well!!