代码之家  ›  专栏  ›  技术社区  ›  Steve Ruiz

asmx webservice不返回json,只能使用application/x-www-form-urlencoded contenttype发布

  •  8
  • Steve Ruiz  · 技术社区  · 15 年前

    如果contenttype=“application/x-www-form-urlencoded;charset=utf-8”,我可以使用jquery调用我的webservice

    但是,这将返回XML: <string>[myjson]</string>

    如果我试图使用“application/json;charset=utf-8”发送到服务,我会收到一个500错误,其中stacktrace和exceptiontype为空。我的webservice函数从未被命中,所以我不太确定如何调试这种情况。

    我的方法和类被适当的属性修饰,并被设置为使用json作为它们的响应类型(就像我的wsdl和disco文件一样)。我在web.config中安装了ajax扩展和必需的条目。

    这是在一个sharepoint服务器场上,但我不确定这会有多大的不同。我在所有wfe上部署了web.config更改,并安装了ajax扩展。同样,该服务工作正常,它只接受默认的内容类型。

    不知道我错过了什么,伙计们…

    我的ajax调用:

    $.ajax({
    type: "POST",
    url: "/_vti_bin/calendar.asmx/Test",
    dataType: "json",
    data: "{}",
    contentType: "application/json; charset=UTF-8",
    success: function(msg){
        alert(msg);
        },
    error: function(xhr, msg){ alert(msg + '\n' + xhr.responseText); }
    });
    

    我的webservice类:

    [WebService(Namespace = "http://namespace")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService()]
    public class CalendarService : WebService
    {
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string Test()
        {
            return "Hello World";
        }
    }
    
    7 回复  |  直到 8 年前
        1
  •  3
  •   Mark Schultheiss    15 年前

    我已经在2.0中使用了web服务,但是我已经设置了对.d的保护(参见下面的datafilter)。我还返回一个对象数组。注意:对象的类是静态的,或者至少对我来说它不能正常工作。

      $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: "{}",
            dataFilter: function(data)
            {
                var msg;
                if (typeof (JSON) !== 'undefined' &&
                    typeof (JSON.parse) === 'function')
                    msg = JSON.parse(data);
                else
                    msg = eval('(' + data + ')');
                if (msg.hasOwnProperty('d'))
                    return msg.d;
                else
                    return msg;
            },
            url: "webservice/ModifierCodesService.asmx/GetModifierList",
            success: function(msg)
            {
                LoadModifiers(msg);
            },
            failure: function(msg)
            {
                $("#Result").text("Modifiers did not load");
            }
        });
    

    以下是我的Web服务的一个片段:

    [WebService(Namespace = "http://mynamespace.com/ModifierCodesService/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    public class ModifierCodesService : System.Web.Services.WebService
    {
    
         /// <summary>
        /// Get a list of Modifiers
        /// </summary>
        /// <returns></returns>
        [WebMethod(EnableSession = true)]
        public Modifier[] GetModifierList()
        {
            return GetModifiers();
        }
           /// <summary>
            /// Modifiers list from database
            /// </summary>
            /// <returns></returns>
            private Modifier[] GetModifiers()
            {
                List<Modifier> modifier = new List<Modifier>();
                ModifierCollection matchingModifiers = ModifierList.GetModifierList();
                foreach (Modifier ModifierRow in matchingModifiers)
                {
                    modifier.Add(new Modifier(ModifierRow.ModifierCode, ModifierRow.Description));
                }
                return modifier.ToArray();
            }
        }
    

    目标代码:

     public static class ModifierList
        {
    
            /// <summary>
            /// Returns the Modifier Collection.
            /// </summary>
            /// <param name="prefix"></param>
            /// <returns></returns>
            public static ModifierCollection GetModifierList()
            {
    
        2
  •  1
  •   Steve Reed Sr    14 年前

    我今天一直在和一个iphone应用程序和一个.net web服务打交道。

    我发现,如果我将内容类型更改为application/jsonrequest,它就会顺利通过,并且我能够在web服务器中处理数据。

    只是为了露齿一笑,我在web.config中添加了上面提到的一行,但它并没有使application/json工作。

        3
  •  0
  •   Bobby Borszich    15 年前

    不确定是否可以这么简单,但我正在使用jquery从我的web方法调用json。

    我看到的主要区别是类的属性

    [系统.web.script.services.scriptservice]

        [WebService(Namespace = "http://MyNameSpace")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.Web.Script.Services.ScriptService]
        public class Web : System.Web.Services.WebService{
    
           [WebMethod]
           public string TestMethod(){
             return "Testing";
           }
    
        }
    

    我不得不假设您使用的是3.5框架,因为这是公开json web方法的唯一方法。

    我的jquery调用看起来几乎相同,所以没有问题。

        4
  •  0
  •   Community datashaman    8 年前

    如果要在IE中测试,请尝试从ContentType属性中删除charset声明(即,它应该如下所示:

    contentType: "application/json",
    

    我还没有找到原因,但是ie在用 charset=UTF-8 “部分。

    alt text

        5
  •  0
  •   marr75    15 年前

    我认为您正在寻找webinvoke或webget属性,它允许您指定uri模板、主体样式、请求和响应格式,例如:

    [WebGet(ResponseFormat= WebMessageFormat.Json)]
    

    This 链接可能会有帮助。webinvoke也有类似的文章(主要用于post)。

        6
  •  0
  •   srmark    15 年前

    我经常使用jquery ajax json调用asmx webservice。它在所有浏览器中都能完美工作。我使用.net 2.0安装了asp.net ajax扩展(捆绑在3.5中)。

    我们班和你们班有同样的装修工人。我的方法只有 [WebMethod(EnableSession = true)] 装饰者。但是,my web.config的httphandlers部分包含以下条目:

    <add verb="*" path="*jsonservice.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    

    我的jquery调用如下所示:

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "path/to/service/jsonservice.asmx/MethodName",
        data: JSON.stringify(DTO),
        dataType: "json",
        success: function(rtrn) { alert("good"); },
        error: function(req, msg, err) { alert("bad"); }
    });
    

    This article 是我知识的根源。

        7
  •  -1
  •   ScottE    15 年前

    看起来您必须在scriptMethod标记中指定json作为响应格式。这是vb.net的,但我相信你会想到:

    responseformat:=响应格式.json