ASMX web服务不返回JSON,只能发布使用应用程序/ x-WWW的形式,进行了urlencoded的contentType [英] ASMX webservice not returning JSON, can only POST using application/x-www-form-urlencoded contentType

查看:800
本文介绍了ASMX web服务不返回JSON,只能发布使用应用程序/ x-WWW的形式,进行了urlencoded的contentType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用jQuery调用我的web服务如果将contentType =应用/的X WWW的形式了urlencoded;字符集= UTF-8



这将,但是,返回XML:<串> myjson] LT; /串>



如果我尝试发布使用服务应用/ JSON;字符集= UTF-8我收到一个500错误与空堆栈跟踪和ExceptionType。我的web服务功能,从不打,所以我不太清楚如何调试这种情况。



我的方法和类的相应属性的装饰,都设置为使用JSON因为他们的反应类型(如做我的WSDL和迪斯科文件)。我已经安装了Ajax的扩展和在web.config中所需的条目。



这是SharePoint场,但我不知道,让太多的差别。我部署的所有WFE的web.config中的变化,以及安装了AJAX扩展。同样的服务工作,它只是将不会接受任何事情,但默认的内容类型。



不知道我在这里失踪,伙计们...



我的Ajax调用:

  $阿贾克斯({
型: POST,
网址:/_vti_bin/calendar.asmx/Test,
数据类型:JSON,
数据:{},
的contentType:应用程序/ JSON;字符集= UTF-8,
成功:函数(MSG){
警报(味精);
},
错误:功能(XHR,味精){警报( MSG +\\\
'+ xhr.responseText);}
});



我的web服务类:

  [WebService的空间(namespace =HTTP://命名空间)] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]
公共类CalendarService:WebService的
{
[的WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
公共字符串测试()
{
返回你好世界;
}
}


解决方案

我在使用Web服务2.0这个工作,但我已经把从.D地方保护(见下文data​​Filter)。我也是返回对象的数组。注意:该类的对象是静态的,或者它不会正确地至少对我来说工作



  $阿贾克斯({ 
型:POST,
的contentType:应用/ JSON的;字符集= UTF-8,
数据:{},
dataFilter:功能(数据)
{
变种味精;
如果(typeof运算(JSON)=='不确定'和;!&安培;
typeof运算(JSON.parse)==='功能')
味精= JSON.parse(数据);
,否则
味精=的eval('('+数据+')');
如果(msg.hasOwnProperty('D'))
返回msg.d;
,否则
返回味精;
},
网址:Web服务/ ModifierCodesService.asmx / GetModifierList,
成功:功能(MSG)
{
LoadModifiers(MSG);
},
失败:函数(MSG)
{
$(#结果)。文本(修饰符没有加载);
}
});

下面是我的web服务的snippett:



...

  [WebService的空间(namespace =http://mynamespace.com/ModifierCodesService/)] 
〔WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(假)]
[ScriptService]
公共类ModifierCodesService:System.Web.Services.WebService
{

///<总结>
///取修饰符
///℃的名单; /总结>
///<&回报GT;< /回报>
[的WebMethod(EnableSession =真)]
公共修改[] GetModifierList()
{
返回GetModifiers();
}
///<总结>从数据库
///<
///修改器列表; /总结>
///<&回报GT;< /回报>
私人修改[] GetModifiers()
{
名单,LT;改性剂GT;修改=新的List<改性剂GT;();
ModifierCollection matchingModifiers = ModifierList.GetModifierList();
的foreach(修改ModifierRow在matchingModifiers)
{
modifier.Add(new修饰符(ModifierRow.ModifierCode,ModifierRow.Description));
}
返回modifier.ToArray();
}
}



...



目标代码:

 公共静态类ModifierList 
{

///<总结>
///返回修改集合。
///< /总结>
///< PARAM NAME =前缀>< /参数>
///<&回报GT;< /回报>
公共静态ModifierCollection GetModifierList()
{


I can call my webservice using jQuery IF the contentType = "application/x-www-form-urlencoded; charset=utf-8"

This will, however, return the xml: <string>[myjson]</string>

If I try to POST to the service using "application/json; charset=utf-8" I receive a 500 error with an empty StackTrace and ExceptionType. My webservice function is never hit so I'm not quite sure how to debug this situation.

My methods and classes are decorated with the appropriate attributes and are set to use JSON as their response type (as do my wsdl and disco files). I have the Ajax extensions installed and the required entries in web.config.

This is on a SharePoint farm, but I'm not sure that makes too much of a difference. I deployed the web.config changes on all WFE's as well as installed the ajax extensions. Again the service works, it just will not accept anything but the default content type.

Not sure what I'm missing here, fellas...

my ajax call:

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

My webservice class:

[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";
    }
}

解决方案

I have this working in 2.0 using web services, but I have put in place protection from the .d (see dataFilter below). I also am returning an array of objects. NOTE: the class for the object is static, or it would not work correctly for me at least.

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

Here is a snippett of my web service:

...

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

...

the object code:

 public static class ModifierList
    {

        /// <summary>
        /// Returns the Modifier Collection.
        /// </summary>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static ModifierCollection GetModifierList()
        {

这篇关于ASMX web服务不返回JSON,只能发布使用应用程序/ x-WWW的形式,进行了urlencoded的contentType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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