Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ResponseFormat.Json returns xml [duplicate]

i know im not the first with this problem, but i cant seem to find a working solution

when using a webservice set to return json, .net still wraps it in an XML wrapper.

i searched and tried many things

  1. i tried adding various httphandler settings to my web.config, as suggested in certain posts, but these had no effect. also i don't think its necessary as im working on a brand new win7/iis7.5/.net4 box. i read that since .net 3.5 there shouldn't be any problem. but there is!
  2. i tried with and without the responseformat.json decoration. my webservice returns valid json ( i can parse it with parsejson, after extracting the string)
  3. i tried explicitly setting the contenttype and datatype. this causes an error complaining that the response was invalid json. which is right!

what is happening is very confusing, in IE , at least on my devbox, the response returns as an xml document where i can just use msg.text and easily get the json string, but in production i tested in FF and it returns as a document, with no "text" property.

heres my javascript/jquery

$.ajax({
error: function (xhr, ajaxOptions, thrownError) {
    alert(xhr.status+'-'+xhr.statusText);
    alert(thrownError);
    },
    url: '<%=ResolveUrl("~/WebService.asmx")%>' + "/JackJill",
    contentType: "application/json",
    success: function (msg) {
        alert(msg.d); 
    }
});

so: how can i simply ask .net to return a valid regular json string, instead of wrapping it. i believe that will solve all the problems. it will also make my service more accessible to the world at large, so they don't have to do any special parsing.

thank you very much for any advice or pointers

sincerely

EDIT: heres a sample webservice that i just tested:

<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> <WebMethod()> Public Function JackJill() As String
    Return "[{""Name"":""Jack""},{""Name"":""Jill""}]"
End Function

then when i put this in the browser

http://localhost:81/webservice.asmx/JackJill

i get

<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://tempuri.org/">
[{"Name":"Jack"},{"Name":"Jill"}]
</string> 

why all this xml stuff here?

like image 645
Yisroel M. Olewski Avatar asked Apr 10 '11 10:04

Yisroel M. Olewski


2 Answers

I realise this question is old but just in case someone else is looking at this answer in the future ....

I have found (according to this http://encosia.com/asmx-and-json-common-mistakes-and-misconceptions/ and this http://weblogs.asp.net/scottgu/archive/2007/04/04/json-hijacking-and-how-asp-net-ajax-1-0-mitigates-these-attacks.aspx) that if you wish to consume JSON from an .ASMX it's necessary to :

  • set the content type to 'application/json' and
  • set the method to 'POST'

Up above Yisman says he doesn't want to restrict his service to POST requests but (according to those two references) it does seem that if you wish to use .ASMX and receive JSON that's what you're going to have to do.

like image 88
shearichard Avatar answered Nov 16 '22 16:11

shearichard


I realise this is somewhat old but I too was banging my head at this problem where even though the output type was specified as Json it was still being wrapped in XML. The problem with the solutions above for me was that I needed for it to be called by a GET.

The solution while not using a web service does the job for me. What I did was just create another aspx page and added it to my project, in the aspx page I built up the json object and did the following (All inside the Page_Load method):

Response.ContentType = "application/json";
Response.Write(JsonObject.ToString());
Response.End();

Then to call the "page" from the client you simply use the URL eg. "http://aspjsonpage.aspx?parameter=value"

And you get the Json you need.

like image 3
Andy Avatar answered Nov 16 '22 15:11

Andy