1

我正在使用 ExtJs 4.0。

我正在尝试从 Web 服务的数据库中获取数据并尝试在网格面板中进行设置,但出现以下错误Ext.Error: Unable to parse the JSON returned by the server: You're trying to decode an invalid JSON String:

extjs 代码:

Ext.application({
    name: 'HelloExt',
    launch: function() {
        // Model definition and remote store (used Ext examples data)
        Ext.define('ForumThread', {
            extend: 'Ext.data.Model',
            fields: ['countryId', 'countryName'],
            idProperty: 'countryId'
        });

        var store = Ext.create('Ext.data.Store', {
            pageSize: 20,
            model: 'ForumThread',
            autoLoad: true,
            proxy: {
                type: 'ajax',
                url: '../reports/report.asmx/display',
                contentType: "application/json; charset=utf-8;",
                 //url: '/grid.json',
                reader: {    
                    root: 'Data',          
                    type: 'json'
                }
            }
        });

        // Define grid that will automatically restore its selection after store reload
        Ext.define('PersistantSelectionGridPanel', {
            extend: 'Ext.grid.Panel'

        });

        // Create instance of previously defined persistant selection grid panel
        var grid = Ext.create('PersistantSelectionGridPanel', {
            autoscroll: true,
            height: 300,
            renderTo: Ext.getBody(),
            //region: 'center',
            store: store,
            multiSelect: true, // Delete this if you only need single row selection
            stateful: true,
            forceFit: true,
            loadMask: false,
            viewConfig: {
                stripeRows: true
            },
            columns:[{
                id: 'countryId',
                text: "countryId",
                dataIndex: 'countryId',
                flex: 1,
                sortable: false
            },{
                text: "countryName",
                dataIndex: 'countryName',
                width: 70,
                align: 'right',
                sortable: true
            } ]
        });
    }
});

报告.asmx

 [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
      [System.Web.Script.Services.ScriptService]
 [Serializable]

        public class report : System.Web.Services.WebService
            {

                [WebMethod]
                public string HelloWorld()
                {
                    return "Hello World";
                }
                [WebMethod]
                [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true, XmlSerializeString = false)]
                public string display()
                {
                     employee obj = new employee();

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                return serializer.Serialize(obj.selectAll_employeeDetail(0, 0, 0));
                }
            }

谁能建议我...我的代码有什么问题?/

4

1 回答 1

2

可能是因为您编写了一些可能不正确的奇怪的自定义 JSON 序列化(删除尾随、结束字符?为什么?)

.NET JSON 序列化很简单:

JavaScriptSerializer serializer = new JavaScriptSerializer();
HttpContext.Response.Write(serializer.Serialize(Object));
HttpContext.Response.ContentType = "application/json";

您必须将 [Serializable] 属性添加到类或使用已经具有该属性的 .NET 类型。

您返回的字符串必须不是有效的 JSON。

[编辑] 奇怪的事情:Asmx 服务(也可能是 WCF 服务,因为我从 microsoft AJAX 脚本逆向工程解决方案)不寻找“接受”标头来定义响应类型,因为它应该导致 RFC 用于 http: http://www .w3.org/Protocols/rfc2616/rfc2616-sec14.html

Accept request-header 字段可用于指定响应可接受的某些媒体类型。

所以这应该足够了,但使用 jQuery.ajax 就不行了。从我上面提到的 AJAX 脚本中,我获取了标头 Content-Type:

“内容类型”:“应用程序/json;charset=utf-8”

用这个 jQuery 工作得很好。所以我想你必须将此标头添加到请求中,这应该可以。奇怪的是 [ScriptMethod] 属性没有将 JSON 的默认响应设置为状态http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.aspx

指定将响应序列化为 JSON 还是 XML。默认值为 Json。您还必须通过此标头强制服务作为脚本服务工作:

http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

Content-Type 请求正文的 MIME 类型(用于 POST 和 PUT 请求)

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Content-Type entity-header 字段表示发送给接收者的 entity-body 的媒体类型

所以我猜微软的人会这样做:“如果你不以 JSON 格式发送,我们也不会把它发送给你。” :)

我希望这会奏效。

PS:因为这是 json 服务,如果您返回 .NET 内置类型或具有 [Serializable] 属性的类型,您可以直接返回它。不需要预先序列化无论如何都会被序列化的响应。

于 2012-11-06T07:18:41.790 回答