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
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?
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 :
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With