而从C#转移到ExtJS的JSON数据的变化 [英] JSON Data changes while transferring from C# to ExtJS

查看:120
本文介绍了而从C#转移到ExtJS的JSON数据的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个WebMethod在asp.net C#通过Ajax调用在ExtJS的4.2.2当传送JSON数据,多个字符添加到字符串的开头和结尾。

JSON数据临行前C#:

  [{ID:0,姓名:ALAN},{ID:1,姓名:BLAKE}]
 

JSON数据所看到的是收到的ExtJS的萤火虫

  {D:[{ID:0,姓名:ALAN},{ID:1,名 布莱克}]}
 

这也会发生,如果JSON数据有一组root属性。 从它出现什么,似乎有什么东西在某处沿线处理传入的数据作为一个JSON字符串或类似的东西的一个变量。

在C#结束code:

  [WebService的(命名空间=本地主机)
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
[System.ComponentModel.ToolboxItem(假)]
[System.Web.Script.Services.ScriptService]
公共类导演:System.Web.Services.WebService
{
    [WebMethod的]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = TRUE,XmlSerializeString = FALSE)]
    公共字符串的getData()
    {
        字符串JSON =[{\ID \:\0 \,\名称\:\ALAN \},{\ID \:\1 \,\名称\: \BLAKE \}];
        System.Diagnostics.Debug.WriteLine(JSON);
        返回JSON;
    }
}
 

code为ExtJS的AJAX调用(已经实施了解决方法):

  Ext.Ajax.request({
    异步:假的,
    网址:Test061014.ApplicationPath +/Director.asmx/getData,
    标题:{内容类型:应用/ JSON的},
    适用范围:此,
    成功:函数(康涅狄格州,响应,期权,eOpt){
        VAR S = conn.responseText;
        S = s.substring(6,(s.length  -  2));
        S = s.replace(/ \\ /克,);
        categoryData = JSON.parse(多个);
    },
});
 

解决方案

这是插入由ASP.NET出于安全原因。请查看这篇文章了解更多详情。

  

如果您不熟悉.D我指的是,这是一个简单的   安全功能,微软增加了在ASP.NET 3.5的版本   ASP.NET AJAX。通过封装父母中的JSON响应   对象,该框架有助于防止一个特别讨厌的XSS   漏洞。

他们有使用 dataFilter 属性,因此你可以不用担心 .D 的一个很好的解决方案。此外,信贷的文章,这里是他们的解决方案。您可能需要阅读文章的不要让我想起部分,因为有几个细节我离开了。

  dataFilter:功能(数据){
//这个沸腾的响应串下来
//插入合适的JavaScript对象()。
变种味精=的eval('('+数据+')');

//如果响应具有.D顶级性能,
//返回什么是低于代替。
如果(msg.hasOwnProperty('D'))
  返回msg.d;
其他
  返回味精;
},
 

When transferring JSON data from a Webmethod in asp.net C# through an Ajax call in ExtJS 4.2.2, several characters are added to the beginning and end of the string.

JSON Data before leaving C#:

[{"ID":"0","NAME":"ALAN"},{"ID":"1","NAME":"BLAKE"}]

JSON Data as seen by firebug which is received by ExtJS

{"d":"[{"ID":"0","NAME":"ALAN"},{"ID":"1","NAME":"BLAKE"}]"}

This will also happen if the JSON Data has a set root property. From what it appears it seems as if something somewhere along the line treated the incoming data as a variable in a JSON string or something like that.

Code on the C# end:

[WebService(Namespace = "localhost")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class Director : System.Web.Services.WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true, XmlSerializeString = false)]
    public string getData()
    {
        string json = "[{\"ID\":\"0\",\"NAME\":\"ALAN\"},{\"ID\":\"1\",\"NAME\":\"BLAKE\"}]";
        System.Diagnostics.Debug.WriteLine(json);
        return json;
    }
}

Code for the ExtJS Ajax Call (already has a workaround implemented):

Ext.Ajax.request({
    async: false,
    url: Test061014.ApplicationPath + '/Director.asmx/getData',
    headers: { 'Content-Type': 'application/json' },
    scope: this,
    success: function (conn, response, options, eOpt) {
        var s = conn.responseText;
        s = s.substring(6, (s.length - 2));
        s = s.replace(/\\/g, "");
        categoryData = JSON.parse(s);
    },
});

解决方案

That is inserted by ASP.NET for security reasons. Check out this article for more details.

If you aren’t familiar with the ".d" I’m referring to, it is simply a security feature that Microsoft added in ASP.NET 3.5’s version of ASP.NET AJAX. By encapsulating the JSON response within a parent object, the framework helps protect against a particularly nasty XSS vulnerability.

They have a nice solution of using the dataFilter property so you can stop worrying about .d. Again, credit to the article, here is their solution. You may need to read the article's Don't make me think section as there are a couple of details I left out.

dataFilter: function(data) {
// This boils the response string down 
//  into a proper JavaScript Object().
var msg = eval('(' + data + ')');

// If the response has a ".d" top-level property,
//  return what's below that instead.
if (msg.hasOwnProperty('d'))
  return msg.d;
else
  return msg;
},

这篇关于而从C#转移到ExtJS的JSON数据的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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