繁体   English   中英

ASMX Web服务不返回JSON,只能使用application / x-www-form-urlencoded contentType进行POST

[英]ASMX webservice not returning JSON, can only POST using application/x-www-form-urlencoded contentType

我可以使用jQuery调用我的Web服务,如果contentType =“ application / x-www-form-urlencoded; charset = utf-8”

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

如果尝试使用“ application / json; charset = utf-8”发布到服务,则会收到500错误,其中包含空StackTrace和ExceptionType。 我的Web服务功能从未被使用过,因此我不确定如何调试这种情况。

我的方法和类都装饰有适当的属性,并设置为使用JSON作为其响应类型(我的wsdl和disco文件也是如此)。 我已经安装了Ajax扩展,并且在web.config中有必需的条目。

这是在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(Namespace = "http://namespace")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]
public class CalendarService : WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string Test()
    {
        return "Hello World";
    }
}

我已经使用Web服务在2.0中进行了此工作,但是我已经对.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()
        {

今天,我一直在通过与.Net Web Service通话的iPhone应用程序来解决这个问题。

我发现,如果我将内容类型更改为application / jsonrequest,则可以顺利通过,并且能够处理Web服务器中的数据。

只是出于笑容,我在我的web.config中添加了上面提到的行,但是它没有使application / json工作。

我对ASMX Web服务使用了JQuery AJAX JSON调用。 它在所有浏览器中均可完美运行。 我正在使用安装了ASP.NET AJAX扩展(捆绑在3.5中)的.NET 2.0。

我班的装饰师和你的一样。 我的方法仅具有[WebMethod(EnableSession = true)]装饰器。 但是,我的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"); }
});

本文是我所学的基础。

不知道这是否可能是如此简单,但是我正在使用jQuery从我的Web方法中回调JSON。

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

[System.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网络方法的唯一方法。

我从jQuery发出的呼叫看起来几乎完全相同,因此没有问题。

如果要在IE中进行测试,请尝试从contentType属性中删除字符集声明(即,它应如下所示:

contentType: "application/json",

我还没有找到原因,但是当使用“ charset=UTF-8 ”部分进行JSON调用时,IE似乎有点曲折。

替代文字

我认为您正在寻找WebInvoke或WebGet属性,它使您可以指定Uri模板,正文样式,请求和响应格式,例如:

[WebGet(ResponseFormat= WebMessageFormat.Json)]

链接可能会有所帮助。 WebInvoke也有类似的文章(主要用于发布)。

看起来您必须在scriptMethod标记中指定json作为响应格式。 这是来自vb.net,但我确定您知道:

ResponseFormat:= ResponseFormat.Json

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM