0

我有一个通用处理程序,它将读取 XML 文件,并将结果作为 JSON 发送到 ajax 调用。当我运行程序时,我得到这个错误:

客户端代码:

$(function () {
$('#getData').click(function () {
    $.ajax({
        type: 'GET',
        datatype: 'json',
        url: 'DynamicHandler.ashx',
        contentType: "application/json; charset=utf-8",

        success: function (result) {

            var property = JSON.parse(result);

            console.log(property);
        }
    });
});

});

服务器端代码:(Handler.ashx)

  public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    var realestate = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("Realestate.xml"));
    var query = from items in realestate.Descendants("Property")
        select new
        {
            Name = items.Attribute("Name").Value,
            Image = items.Attribute("Image").Value,
            Location = items.Attribute("Location").Value,
            Rooms = items.Attribute("Rooms").Value,
            PropertyValue = items.Attribute("PropertyValue").Value,
            Contact = items.Attribute("Contact").Value,
            Description = items.Attribute("Description").Value
        };


    var scriptSerializer = new JavaScriptSerializer();
    context.Response.Write(scriptSerializer.Serialize(query));
}

public bool IsReusable
{
    get
    {
        return false;
    }
}

}

链接到 XML 文件:

[http://omerbuzo.me/Realestate.xml][1]

当我使用调试器运行此程序时,我在 Handler.ashx 文件中收到以下错误(在以下行:选择新 {匿名对象});

Object reference not set to an instance of an object.

在 console.log 我得到:

Failed to load resource: the server responded with a status of 500 (Internal Server Error) 

谁能指出似乎是什么问题?

提前谢谢你:)

4

1 回答 1

0

改变

var query = from items in realestate.Descendants("Property")
    select new
    {
        Name = items.Attribute("Name").Value,
        Image = items.Attribute("Image").Value,
        Location = items.Attribute("Location").Value,
        Rooms = items.Attribute("Rooms").Value,
        PropertyValue = items.Attribute("PropertyValue").Value,
        Contact = items.Attribute("Contact").Value,
        Description = items.Attribute("Description").Value
    };

var query = from prop in realestate.Descendants("Property")
    select new
    {
        Name = (string)prop.Element("Name"),
        Image = (string)prop.Element("Image"),
        Location = (string)prop.Element("Location"),
        Rooms = (string)prop.Element("Rooms"),
        PropertyValue = (string)prop.Element("PropertyValue"),
        Contact = (string)prop.Element("Contact"),
        Description = (string)prop.Element("Description")
    };
于 2013-08-03T16:31:23.143 回答