Can I return JSON from an .asmx Web Service if the ContentType is not JSON?
- by Click Ahead
I would like to post a form using ajax and jquery to a .asmx webservice and return the value from the webservice as JSON.
I'm using ASP.NET 4.0. I know that in order to return JSON from a webservice the following needs to be set (1) dataType: "json" (2) contentType: "application/json; charset=utf-8", (3) type: "POST" (4) set the Data to something. I have tested this and it works fine (i.e. my webservice returns the data as JSON) if all *four are set*.
But, lets say in my case I want to do a standard form post i.e. test1=value1&test2=value2
so the contentType is not JSON but I want back JSON {test1:value1}. This doesn't seem to work because the contentType is "application/x-www-form-urlencoded" not "application/json; charset=utf-8".
Can anyone tell me why I can't do this? It seems crazy to me that you have to explicitly
send JSON to get JSON back, yet if you don't use JSON (i.e. post urlencoded contenttype) then the webservice will return XML.
Any insights are greatly appreciated :)