问题
I am using JQuery EasyUI's datagrid is a very basic implementation.
If I use the inline approach to loading in the data all is fine and the data shows (note the JSON API is working as I have enabled CORS on the server so that's not the issue):
<table class="easyui-datagrid"
data-options="url:'https://www.driverlive.co.uk/rest/api/PushMessage/GetPushDriverMessagesList?DeviceId=a99f8a977696bfb9&DateFrom=2014-10-27T00:00:00&DateTo=2015-11-11T00:00:00',method:'get',singleSelect:true,fit:true,fitColumns:true">
<thead>
<tr>
<th data-options="field:'PushDriverMessageDBID'" width="80">Item ID</th>
<th data-options="field:'PushDriverMessageGuid'" width="100">PushDriverMessageGuid</th>
<th data-options="field:'AppKey',align:'right'" width="80">AppKey</th>
<th data-options="field:'PushNotificationMessage',align:'right'" width="300">PushNotificationMessage</th>
<th data-options="field:'Sender'" width="150">Sender</th>
<th data-options="field:'MessageDated',align:'center'" width="50">MessageDated</th>
</tr>
</thead>
</table>
If I attempt the JavaScript way of loading the datagrid I get no data but I cannot understand why?
// Dispatching DataGrid
var JSONDispatchingURL = "https://www.driverlive.co.uk/rest/api/PushMessage/GetPushDriverMessagesList?DeviceId=a99f8a977696bfb9&DateFrom=2014-10-27T00:00:00&DateTo=2015-11-11T00:00:00";
$('#DGDispatching').datagrid({
url: JSONDispatchingURL,
columns: [[
{ field: 'PushDriverMessageDBID', title: 'PushDriverMessageDBID', width: 100 },
{ field: 'PushDriverMessageGuid', title: 'PushDriverMessageGuid', width: 100 },
{ field: 'AppKey', title: 'AppKey', width: 100 },
{ field: 'PushNotificationMessage', title: 'PushNotificationMessage', width: 100 },
{ field: 'Sender', title: 'Sender', width: 100 },
{ field: 'MessageDated', title: 'MessageDated', width: 100}
]]
});
I have the same result with the JSFiddle here: http://jsfiddle.net/b6fxs2v2/1/
I have followed the instructions here: http://www.jeasyui.com/documentation/datagrid.php so I'm a bit lost...
回答1:
OK. I fired up Fiddler and it showed that JQuery EasyUI was calling the service with POST rather than GET. The REST API was therefore responding with a 405. The response was specifically {"Message":"The requested resource does not support http method 'POST'."}
I found similar this SO post and then modified my code as below. This then worked!
var JSONDispatchingURL = "https://www.driverlive.co.uk/rest/api/PushMessage/GetPushDriverMessagesList?DeviceId=a99f8a977696bfb9&DateFrom=2014-10-27T00:00:00&DateTo=2015-11-11T00:00:00";
$('#DGDispatching').datagrid({
url: JSONDispatchingURL,
method: 'get',
columns: [[
{ field: 'PushDriverMessageDBID', title: 'PushDriverMessageDBID', width: 100 },
{ field: 'PushDriverMessageGuid', title: 'PushDriverMessageGuid', width: 100 },
{ field: 'AppKey', title: 'AppKey', width: 100 },
{ field: 'PushNotificationMessage', title: 'PushNotificationMessage', width: 100 },
{ field: 'Sender', title: 'Sender', width: 100 },
{ field: 'MessageDated', title: 'MessageDated', width: 100 }
]]
});
来源:https://stackoverflow.com/questions/28463014/cannot-load-jquery-easyui-datagrid-using-javascript-approach