当从Web API GET请求加载数据时,我从FireFox获得了最奇怪的行为,而其他浏览器在这方面做得很好。以下是Fiddler在我使用Firefox时的提示:
3817 500 HTTP localhost:52543 /api/Tasks/Get?_dc=1442848131483&page=1&start=0&limit=25 5 403 private application/xml; charset=utf-8 firefox:138020
同样的动作,其他浏览器(Chrome):
3954 200 HTTP localhost:52543 /api/Tasks/Get?_dc=1442848159073&page=1&start=0&limit=25 1 508 no-cache; Expires: -1 application/json; charset=utf-8 chrome:2808
我无法捕获Application_Error中的错误,也无法在客户端的异常侦听器中接收到错误,因此我怀疑在将结果返回给客户端和客户端处理结果之间出现了错误,但我完全不知道问题可能发生在哪里。
下面是store的定义:
Ext.define('SchedulerApp.store.UnplannedTaskStore', {
extend: 'Ext.data.Store',
model: 'UnplannedTask',
autosync: false,
autoLoad: true,
proxy: {
type: 'rest',
api: {
read: '/api/Tasks/Get',
add: '/api/Tasks/Add',
update: '/api/Tasks/Update',
destroy: '/api/Tasks/Destroy'
},
actionMethods: {
create: 'POST',
read: 'GET',
update: 'POST',
destroy: 'POST'
},
reader: {
type: 'json',
rootProperty: 'data',
totalProperty: 'total'
},
writer: {
type: 'json',
writeAllFields: true
}
},
listeners: {
load: function (sender, node, records) {
},
exception: function (proxy, response, options) {
Ext.MessageBox.alert('Error', response.status + ": " + response.statusText);
}
}
});
和模型:
Ext.define('UnplannedTask', {
extend: 'Ext.data.Model',
fields: [
{ name: 'Importance', type: 'float' },
{ name: 'Category', type: 'string' },
{ name: 'TaskNo', type: 'float' }
]
});
这是我在Web API中的内容:
[System.Web.Http.HttpGet]
public async Task<dynamic> Get(string page, string start, string limit)
{
// Get items from database with request information from the Kendo Grid Control
PagingResult<TaskViewModel> tasks = await this.Worker.GetPagedTasksAsync(int.Parse(page), int.Parse(limit), null, null);
// Map them to store objects
var convertedTasks = new SchedulerTasksViewModel()
{
total = tasks.Count,
data = tasks.Items.Select(x => new SchedulerTask()
{
Importance = x.Importance,
Category = x.Category,
TaskNo = x.TaskNumber
}).ToArray()
};
var response = Request.CreateResponse(HttpStatusCode.OK, convertedTasks);
return response;
}
这可能是一个浏览器的问题,还是我错过了一些在服务器端?
尝试将此标头添加到代理:
headers: {'Content-Type': "application/json" }
我想详细说明一下。错误由XML序列化器抛出;您看不到详细信息,因为IIS不会将它们发送到前端。
我建议修改你所有的API调用,使它们也能处理XML——即使你的前端不使用XML。如果您可以在新的浏览器选项卡中打开API调用,并且XML序列化器不会用序列化错误掩盖代码错误,则调试起来要容易得多。
要查看错误消息,您必须允许您的开发IIS将错误显示在前面:
- 打开IIS7管理器
- 选择网站,在其功能视图,双击"错误页面"。
- 右键单击并选择"编辑功能设置…"或从操作窗格(在右侧)选择相同的
- 选择"详细错误"单选按钮并点击OK
(源)
我最好的猜测是,你只需要用[DataContract]
, [DataContractAttribute]
或[DataMemberAttribute]
来装饰某些类型或属性。错误信息会告诉你哪些是需要装饰的,以及如何装饰。
另一件事:如果您使用多个Ajax请求,我建议在Ajax代理上定义覆盖。这样你就不会忘记了:
Ext.define("MyApp.override.Ajax", {
override:'Ext.data.proxy.Ajax',
headers:{'Accept':'application/json'}
});