Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to return JSON from a 2.0 asmx web service
    primarykey
    data
    text
    <p>I am using .Net framework 2.0 / jQuery to make an Ajax call to a 2.0 web service. No matter what I set the contentType to in the ajax call, the service always returns XML. I want it to return Json!</p> <p>Here is the call:</p> <pre><code> $(document).ready(function() { $.ajax({ type: "POST", url: "DonationsService.asmx/GetDate", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { // Hide the fake progress indicator graphic. $('#RSSContent').removeClass('loading'); // Insert the returned HTML into the &lt;div&gt;. $('#RSSContent').html(msg.d); } }); }); </code></pre> <p>Here is what the request header looks like in Fiddler:</p> <pre><code>POST /DonationsService.asmx/GetDate HTTP/1.1 x-requested-with: XMLHttpRequest Accept-Language: en-us Referer: http://localhost:1238/text.htm Accept: application/json, text/javascript, */* Content-Type: application/json; charset=utf-8 Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; eMusic DLM/4; .NET CLR 2.0.50727) Host: localhost:1238 Content-Length: 2 Connection: Keep-Alive Pragma: no-cache </code></pre> <p>I have tried setting the contentType to 'text/json' and get the same results.</p> <p>Here is the web service method:</p> <pre><code>&lt;WebMethod()&gt; _ Public Function GetDate() As String 'just playing around with Newtonsoft.Json Dim sb As New StringBuilder Dim sw As New IO.StringWriter(sb) Dim strOut As String = String.Empty Using jw As New JsonTextWriter(sw) With jw .WriteStartObject() .WritePropertyName("DateTime") .WriteValue(DateTime.Now.ToString) .WriteEndObject() End With strOut = sw.ToString End Using Return strOut End Function </code></pre> <p>and here is what it returns:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;string xmlns="http://DMS.Webservices.org/"&gt;{"DateTime":"11/13/2008 6:04:22 PM"}&lt;/string&gt; </code></pre> <p>Does anyone know how to force the web service to return Json when I ask for Json?</p> <p>Please don't tell me to upgrade to .Net Framework 3.5 or anything like that (I'm not that stupid). I need a 2.0 solution.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload