代码之家  ›  专栏  ›  技术社区  ›  mwjackson

如何从ASP.NET AJAX正确格式化JSON响应(对于jQuery)?

  •  0
  • mwjackson  · 技术社区  · 16 年前

    伙计们,我的应用程序客户端的对象序列化似乎无法正常工作?当我在jQuery中使用getJSON()方法时,响应mime类型为text/json(使用json.Net库序列化),它工作得很好,但当我将其移动到ajax()请求时(以便我可以在我的页面上的webmethods中发布代码),它就崩溃了。任何想法都将不胜感激。

    编辑:我使用的是Framework2.0,不是3.5

    我的javascript如下所示:

        function companySearch(txt) 
        {
            $("#<%= divCompanyResult.ClientID %>").hide();
            $("#<%= divCompanyResult.ClientID %>").html("");
    
            var strCompanySearch = $("#<%= tbCompanySearch.ClientID %>").val();
    
            $.ajax
            ({
                type: "POST",
                url: "Home.aspx/GetCompanies", 
                contentType: "application/json; charset=utf-8", 
                datatype: "json",
                data: "{name: '" + strCompanySearch + "'} ", 
                success: companySearchSuccess,
                error: onError
            });
    
            return (true);
        }
    
        function companySearchSuccess(response)
        {
            $.each(response, 
                function(i, company) 
                {
                    $("#<%= divCompanyResult.ClientID %>").prepend(company.Name + "<br />");
                });
    
            $("#<%= divCompanyResult.ClientID %>").slideDown(1000);
        }
    

    my.ASPX页面(注意,它是一个页面,而不是一个Web服务)如下所示:

        [WebMethod]
        public static Company[] GetCompanies(string name)
        {
            Company[] companies = Company.FindCompanies(name);
    
            return companies;
        }
    

    [ActiveRecord]
    public class Company : ActiveRecordBase<Company>
    {
        private int iD;
        private string name;
        private string accountNo;
        private string streetAddr1;
        private string streetAddr2;
        private string streetSuburb;
        private string streetCity;
        private string postAddr1;
        private string postAddr2;
        private string postSuburb;
        private string postState;
        private string postPC;
        private string accountType;
        private string accountSubType;
        private string areaRep;
        private string status;
        private string overview;
        private string bpcsId;
        private string modifiedBy;
        private DateTime modifiedDate;
        private IList<Contact> contacts;
    
        [PrimaryKey]
        public int ID { get { return this.iD; } set { this.iD = value; } }
        [Property]
        public string Name { get { return this.name; } set { this.name = value; } }
        [Property]
        public string AccountNo { get { return this.accountNo; } set { this.accountNo = value; } }
        [Property]
        public string StreetAddr1 { get { return this.streetAddr1; } set { this.streetAddr1 = value; } }
        [Property]
        public string StreetAddr2 { get { return this.streetAddr2; } set { this.streetAddr2 = value; } }
        [Property]
        public string StreetSuburb { get { return this.streetSuburb; } set { this.streetSuburb = value; } }
        [Property]
        public string StreetState { get { return this.streetCity; } set { this.streetCity = value; } }
        [Property]
        public string StreetPC { get { return this.streetCity; } set { this.streetCity = value; } }
        [Property]
        public string PostAddr1 { get { return this.postAddr1; } set { this.postAddr1 = value; } }
        [Property]
        public string PostAddr2 { get { return this.postAddr2; } set { this.postAddr2 = value; } }
        [Property]
        public string PostSuburb { get { return this.postSuburb; } set { this.postSuburb = value; } }
        [Property]
        public string PostState { get { return this.postState; } set { this.postState = value; } }
        [Property]
        public string PostPC { get { return this.postPC; } set { this.postPC = value; } }
        [Property]
        public string AccountType { get { return this.accountType; } set { this.accountType = value; } }
        [Property]
        public string AccountSubType { get { return this.accountSubType; } set { this.accountSubType = value; } }
        [Property]
        public string AreaRep { get { return this.areaRep; } set { this.areaRep = value; } }
        [Property]
        public string Status { get { return this.status; } set { this.status = value; } }
        [Property]
        public string Overview { get { return this.overview; } set { this.overview = value; } }
        [Property]
        public string BPCSId { get { return this.bpcsId; } set { this.bpcsId = value; } }
        [Property]
        public string ModifiedBy { get { return this.modifiedBy; } set { this.modifiedBy = value; } }
        [Property]
        public DateTime ModifiedDate { get { return this.modifiedDate; } set { this.modifiedDate = value; } }
    
        // Inverse ensures is read-only ie. Contact controls the relationship
        // Castle will usually infer relationship, but we explicitly set just to be on the safe side
        [HasMany(Inverse=true, Table="Contact", ColumnKey="CompanyId")] 
        [ScriptIgnore]
        public IList<Contact> Contacts { get { return this.contacts; } set { this.contacts = value; } }
    
        protected Company() { }
    
        public Company(string Name, string StreetAddr1)
        {
            this.Name = Name;
            this.StreetAddr1 = StreetAddr1;
    
            ModifiedBy = "Test";
            ModifiedDate = DateTime.Now;
        }
    
        public static Company[] FindCompanies(string name)
        {
            return FindAll(Expression.InsensitiveLike("Name", "%" + name + "%"));
        } 
    }
    
    2 回复  |  直到 16 年前
        1
  •  3
  •   mwjackson    16 年前

    事实证明,我在ajax请求中忽略了dataType属性的camel大小写

        $.ajax
        ({
            type: "POST",
            url: "Home.aspx/GetCompanies", 
            contentType: "application/json; charset=utf-8", 
            datatype: "json",
            data: "{name: '" + strCompanySearch + "'} ", 
            success: companySearchSuccess,
            error: onError
        });
    

    解决方案

        $.ajax
        ({
            type: "POST",
            url: "Home.aspx/GetCompanies", 
            contentType: "application/json; charset=utf-8", 
            dataType: "json",
            data: "{name: '" + strCompanySearch + "'} ", 
            success: companySearchSuccess,
            error: onError
        });
    

        2
  •  1
  •   tsimon    16 年前

    function companySearchSuccess(response)
    {
      // Get encoded data
      response = response.d; // you could rename this, or just change the variable reference
    
    }
    

    这样做是为了防止一种特殊的javascript攻击。你可以看到更多的细节 here here .