我正在使用.Net framework 2.0 / jQuery對2.0 Web服務進行Ajax調(diào)用。無論我在ajax調(diào)用中將contentType設置為什么,該服務始終返回XML。我希望它返回Json!這里是電話: $(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 <div>. $('#RSSContent').html(msg.d); } }); }); 這是Fiddler中的請求標頭的樣子:POST /DonationsService.asmx/GetDate HTTP/1.1x-requested-with: XMLHttpRequestAccept-Language: en-usReferer: http://localhost:1238/text.htmAccept: application/json, text/javascript, */*Content-Type: application/json; charset=utf-8Accept-Encoding: gzip, deflateUser-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:1238Content-Length: 2Connection: Keep-AlivePragma: no-cache我嘗試將contentType設置為'text / json'并獲得相同的結(jié)果。這是Web服務方法:<WebMethod()> _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 strOutEnd Function這是它返回的內(nèi)容:<?xml version="1.0" encoding="utf-8"?><string xmlns="http://DMS.Webservices.org/">{"DateTime":"11/13/2008 6:04:22 PM"}</string>有人知道我要Json時如何強制Web服務返回Json嗎?請不要告訴我升級到.Net Framework 3.5或類似的東西(我不是那么愚蠢)。我需要一個2.0解決方案。
如何從2.0 asmx Web服務返回JSON
慕碼人8056858
2019-11-18 13:16:46