如何从2.0 ASMX Web服务返回JSON [英] How to return JSON from a 2.0 asmx web service

查看:302
本文介绍了如何从2.0 ASMX Web服务返回JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET Framework 2.0 / jQuery来进行Ajax调用到2.0 Web服务。无论我在Ajax调用设置的contentType于服务始终返回XML。我希望它返回JSON!

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!

下面是呼叫:

      $(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);
            }
          });
        });    

下面是请求头看起来像小提琴手:

Here is what the request header looks like in Fiddler:

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

我已经尝试设置的contentType为'text / JSON,并得到了相同的结果。

I have tried setting the contentType to 'text/json' and get the same results.

下面是Web服务方法:

Here is the web service method:

<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 strOut

End Function

和这里是它返回:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://DMS.Webservices.org/">{"DateTime":"11/13/2008 6:04:22 PM"}</string>

有谁知道如何强制web服务返回JSON,当我问对JSON?

Does anyone know how to force the web service to return Json when I ask for Json?

请不要告诉我升级到.NET Framework 3.5的或类似的东西(我又不是傻瓜)。我需要一个2.0的解决方案。

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.

推荐答案

这是没有问题的<一个href="http://encosia.com/2010/03/08/asmx-scriptservice-mistakes-installation-and-configuration/">return JSON从ASMX服务在ASP.NET 2.0 。你只需要安装ASP.NET AJAX扩展。

It's no problem to return JSON from ASMX services in ASP.NET 2.0. You just need the ASP.NET AJAX Extensions installed.

请务必将[ScriptService]装饰添加到您的Web服务。这就是指示ASP.NET AJAX框架返回的JSON格式正确的请求的服务器端部分。

Do be sure to add the [ScriptService] decoration to your web service. That's what instructs the server side portion of the ASP.NET AJAX framework to return JSON for a properly formed request.

此外,你需要删除从msg.d的.D在我的例子中,如果您使用的是2.0。 的.D是附带3.5安全功能

Also, you'll need to drop the ".d" from "msg.d" in my example, if you're using it with 2.0. The ".d" is a security feature that came with 3.5.

这篇关于如何从2.0 ASMX Web服务返回JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆