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

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

问题描述

我正在使用 .Net framework 2.0/jQuery 对 2.0 Web 服务进行 Ajax 调用.无论我在 ajax 调用中将 contentType 设置为什么,服务始终返回 XML.我想让它返回 Json!

这是电话:

 $(document).ready(function() {$.ajax({类型:POST",url: "DonationsService.asmx/GetDate",数据: "{}",contentType: "application/json; charset=utf-8",数据类型:json",成功:功能(味精){//隐藏假进度指示器图形.$('#RSSContent').removeClass('loading');//将返回的 HTML 插入到 

中.$('#RSSContent').html(msg.d);}});});

以下是 Fiddler 中的请求标头:

POST/DonationsService.asmx/GetDate HTTP/1.1x-requested-with: XMLHttpRequest接受语言:en-us引用:http://localhost:1238/text.htm接受:应用程序/json、文本/javascript、*/*内容类型:应用程序/json;字符集=utf-8接受编码:gzip、deflate用户代理:Mozilla/4.0(兼容;MSIE 6.0;Windows NT 5.1;SV1;.NET CLR 1.1.4322;eMusic DLM/4;.NET CLR 2.0.50727)主机:本地主机:1238内容长度:2连接:保持活动编译指示:无缓存

我尝试将 contentType 设置为 'text/json' 并获得相同的结果.

这里是网络服务方法:

_公共函数 GetDate() 作为字符串'只是在玩 Newtonsoft.JsonDim sb As New StringBuilderDim sw As New IO.StringWriter(sb)Dim strOut As String = String.Empty使用 jw 作为新的 JsonTextWriter(sw)与 jw.WriteStartObject().WritePropertyName("日期时间").WriteValue(DateTime.Now.ToString).WriteEndObject()结束于strOut = sw.ToString结束使用返回字符串输出结束函数

这是它返回的内容:

有谁知道在我请求 Json 时如何强制 Web 服务返回 Json?

请不要告诉我升级到 .Net Framework 3.5 或类似的东西(我没那么蠢).我需要一个 2.0 解决方案.

解决方案

从 ASP.NET 2.0 中的 ASMX 服务返回 JSON.您只需要安装 ASP.NET AJAX 扩展.

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

此外,如果您在 2.0 中使用它,则在我的示例中,您需要从msg.d"中删除.d"..d"是一种安全性3.5 附带的功能.

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!

Here is the call:

      $(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

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

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

and here is what it returns:

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

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

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.

解决方案

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

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.

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天全站免登陆