我實現在那裏我調用API來獲取一些數據做出反應/終極版的應用程序。該API使用F#編寫,並使用Suave.io。在我的後端,對於一些API調用,我返回一個DataSet(.NET類型)。它包含許多具有多列的DataTable。這些列的名稱來自數據庫。當我從API獲取數據時,Suave.io將MIME類型設置爲JSON,因此它將DataSets作爲JSON對象傳遞給視圖。該視圖除了將其列名設置爲數據庫名稱的DataTables之外,其他所有數據都正確。例如,如果名稱是「IND.APPL」,那麼在視圖中它將是「inD.APPL」。我不知道爲什麼會發生這種情況。WebServer的JSON響應更改鍵名稱爲對象
呼叫到後端獲取數據:
export function loadIndicesDataAPI(data) {
return function(dispatch) {
dispatch(beginAjaxCall());
return fetch(`http://localhost:8083/indices`, {
method: 'post',
header: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => {
return response.json();
})
.then(dataRes => {
dispatch(loadIndicesDataSuccess(dataRes));
}).catch(error => {
dispatch(ajaxCallError(error))
throw(error);
});
};
}
倜儻API代碼片段:
type indicesForm = JsonProvider<""" { "data": [{ "shortName": "s", "name": "n", "symbolId": 1, "isAdded": true, "isDatabase": true, "isReturn": false }], "startdate": "2010-01-01", "enddate": "2011-01-01", "rebalance": 0, "observationAnalysis": 1 } """>
[<AutoOpen>]
module RestFul =
type RestInit = {
RefreshAPI : IndexItem [] * DateTime * DateTime * int * int -> DataSet
}
let JSON v =
let jsonSerializerSettings = new JsonSerializerSettings()
jsonSerializerSettings.ContractResolver <- new CamelCasePropertyNamesContractResolver()
JsonConvert.SerializeObject(v, jsonSerializerSettings)
|> OK
>=> Writers.setMimeType "application/json; charset=utf-8"
let fromJson<'a> json =
let indicesFormJson = indicesForm.Parse(json)
let indexItems = Array.init (indicesFormJson.Data.Length) (fun ind ->
let data = indicesFormJson.Data.[ind]
let indexItemNew = new IndexItem(data.SymbolId, data.Name, data.ShortName, data.IsReturn)
if data.IsAdded then indexItemNew.Add()
if data.IsDatabase then indexItemNew.Imported()
indexItemNew)
let startDate = indicesFormJson.Startdate
let endDate = indicesFormJson.Enddate
let rebalance = indicesFormJson.Rebalance
let observationAnalysis = indicesFormJson.ObservationAnalysis
indexItems, startDate, endDate, rebalance, observationAnalysis
let getResourceFromReq<'a> (req : HttpRequest) =
let getString rawForm =
System.Text.Encoding.UTF8.GetString(rawForm)
req.rawForm |> getString |> fromJson<'a>
let restInit resourceName resource =
let resourcePath = "/" + resourceName
path resourcePath >=> choose [
POST >=> request (getResourceFromReq >> resource.RefreshAPI >> JSON)
]
[<EntryPoint>]
let main argv =
let indicesWebPart = restInit "indices" {
RestInit.RefreshAPI = RefreshAPI
}
startWebServer defaultConfig (choose [indicesWebPart])
0
的另一件事是,列名以大寫字母和他們在成爲小寫前端。
感謝您澄清這一點。你應該如何通過react/redux來獲取數據? IND.AAPL - > inD.AAPL仍然是CamelCase? – Kafo
@HusseinAlkaf我實際上並不知道,但正如我所說的,我認爲這個想法是推送數據,並像從服務器發送數據(在客戶端)那樣「訂閱」。我最好不要再說了。至少,直到我讀了,並嘗試了一些;-) –