ResponseFormat.Json returns xml [duplicate]
Asked Answered
T

4

5

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?

Tompion answered 10/4, 2011 at 10:22 Comment(5)
I am guessing your service, WebService.asmx, is returning XML. Can you provide details about it? Code and configuration would be helpful.Suisse
thanks paulczy, i just added some sample code to my original post. thank you for taking an interest!Tompion
Check out this blog post: blog.prabir.me/post/JSON-in-Classical-Web-Services-ASMX.aspxSuisse
i have already read that post previously. that is why i tried adding the contenttype "application/json" to the ajax call. but that leads to a 500-internal server error. and without setting the contenttype, then msg becomes an xml doc in IE. btw, if i want to make this webservice easily accessible via "GET", it shouldn't depend on the contenttype, as im not aware of any GET requests that need to set the contentType to get a correct responseTompion
i also now added a suggestion in msconnect connect.microsoft.com/VisualStudio/feedback/details/676218/…Tompion
E
8

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.

Extenuation answered 17/6, 2011 at 12:4 Comment(1)
a shame, but very true. as ive already mentioned in my comment to Paulczy's answer.Tompion
W
3

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.

Whitten answered 4/5, 2012 at 14:45 Comment(1)
true enough, ive also used the response.end hack. but ive since moved to WCF, and haven't look back yet ;-)Tompion
W
1

Hi i just have the same problem when using a webservice set to return json, .net still wraps it in an XML wrapper. And my teacher tells me the correcta answer, that is to reinstalr tha .NET framework 4.0 and all problems was solve. thans José Beas!!!!

Wager answered 28/6, 2012 at 8:22 Comment(0)
H
1

Very similar issue with many hours involved. I was using .net2.0 and the web services would not work on the production server running Windows Server 2008 R2 running IIS 7.5. All the issues with the web service returning XML had to deal with the web.config. I build an ajax based web application and copied the config file over to my config and it worked perfectly. The lines of code that needed to be updated are below:

<configSections>
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
            <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
            </sectionGroup>
        </sectionGroup>
    </sectionGroup>
</configSections>

in the web.server:

<httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
    </httpHandlers>
    <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpModules>

and:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules>
        <remove name="ScriptModule"/>
        <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </modules>
    <handlers>
        <remove name="WebServiceHandlerFactory-Integrated"/>
        <remove name="ScriptHandlerFactory"/>
        <remove name="ScriptHandlerFactoryAppServices"/>
        <remove name="ScriptResource"/>
        <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </handlers>
</system.webServer>

I installed Microsoft Visual Web Developer 2008 on the production server and created a new web site, then selected the ASP.NET Web Service. I figured that if you create a project on the production server and it works, the config should have everything correct. Hope this helps.

Hibernia answered 21/8, 2012 at 4:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.