为什么Web服务返回的数据作为msg.d [英] Why does web service return data as msg.d

查看:214
本文介绍了为什么Web服务返回的数据作为msg.d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我的webmethod作为

返回数据

  {D:
 [
   {ID:1,itemtxt:大师},
   {ID:2,itemtxt:交易},
   {ID:3,itemtxt:米施报告。}
 ]
}

如果您发现,该数组被命名为 D 。这是为什么 ?是有它的规则?

有关您的信息,我返回对象的列表(列表与LT;的webitem>

 公共类的webitem
{
    公众的webitem(字符串小子,串KVAL)
    {
        this.id =孩子;
        this.itemtxt = KVAL;
    }    公共字符串ID {搞定;组; }
    公共字符串itemtxt {搞定;组; }
}

这是什么的 D 是什么意思?这将是永诺同为任何数据从我的webmethod派?或者它会根据我的数据类型/类类型的改变?

修改

下面是将WebMethod

  [的WebMethod]
公开名单<&的webitem GT; GetSubModules(字符串键)
{
    字符串s =由SMID秩序;
    如果(关键!= NULL)
        S =,其中的moduleId =+键+ S;
    返回Utility.GetDDLVal(选择SMID ID,smname itemtxt从m_submodules+ S,NULL,NULL);
}


解决方案

这是一个有点安全功能作为ASP.NET的一部分。它总是存在的,只要你不改变序列化。 大卫·沃德解释它是什么:


  

这是所有ASMX服务JSON通过ASP.NET AJAX扩展在ASP.NET 3.5序列化的情况。即使你只返回一个标量返回值,如字符串,int或布尔值,结果将始终是D括起来的。


和的原因是:


  

{D:1}


  
  

是不是有效的JavaScript语句,其中因为这样:


  
  

[1]


  
  


  
  

所以字符串作为脚本的D参数prevents直接执行的包装。任何对象或数组构造函数的后顾之忧。


I found that my webmethod is returning data as

{ "d":
 [
   {"id":"1","itemtxt":"Masters"},
   {"id":"2","itemtxt":"Transactions"},
   {"id":"3","itemtxt":"Misch. Reports"}
 ]
}

If you notice, the array is named as "d". Why is that ? is there any rule for it?

For your information I am returning a list of objects (List<webitem>)

public class webitem
{
    public webitem(string kid, string kval)
    {
        this.id = kid;
        this.itemtxt = kval;
    }

    public string id { get; set;  }
    public string itemtxt { get; set; }
}

What does this "d" mean ? will it allways be same for whatever data i send from my webmethod? or it is going to change depending on my data type/class type?

EDIT

Here is the webmethod

[WebMethod]
public  List<webitem> GetSubModules(string key)
{
    string s = " order by smid";
    if (key != null)
        s = " where moduleid=" + key + s;
    return Utility.GetDDLVal("Select smid id,smname itemtxt from m_submodules "+s, null, null);
}

解决方案

It's a bit of a security feature as part of ASP.NET. It will always be there as long as you don't change serializers. David Ward explains what it is:

This is the case with all ASMX services JSON serialized through the ASP.NET AJAX Extensions in ASP.NET 3.5. Even if you’re only returning a scalar return value, such as a string, int, or boolean, the result will always be enclosed within the "d".

And the why:

{"d": 1 }

Is not a valid JavaScript statement, where as this:

[1]

Is.

So the wrapping of the "d" parameter prevents direct execution of the string as script. No Object or Array constructor worries.

这篇关于为什么Web服务返回的数据作为msg.d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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