1

在我的控制器文件中,我有一个方法可以读取传入的 HTTP 请求,从数据库中读取用户数据,将结果编码为 JSON(使用 jsx)并将其作为响应发送。

    sensorusersdetails('GET', []) ->
        Headers = [{'Access-Control-Allow-Origin', "*"},
        {'Access-Control-Allow-Methods',  "GET, OPTIONS"},
        {'Content-Type',  "application/json"},
        {'Access-Control-Allow-Headers', "X-Requested-With"},
        {'Access-Control-Max-Age', "180"}],   
        Building =  Req:query_param("bld"),
        io:format("User Data request from Node.js server~n~p~n", 
        [Req:query_params()]),
        {{Year,Month,Day},{_,_,_}} = erlang:localtime(),
        StrDate = lists:flatten(io_lib:format("~4..0w-~2..0w-~2..0w",
        [Year,Month,Day])),
        BUserDataList = boss_db:find(sensoruser_data, [{building, 'equals', Building}]),
        io:format("Current Users Data stored in the database: ~n~p~n",[BUserDataList]),

        MyUserJSONList = sensor_preapre_data(BUserDataList, StrDate),
        io:format("The Present Date Sensor Users Data with Binary 1: ~n~p~n",[MyUserJSONList]),
        MyUserJSONListLength = length(MyUserJSONList),
        if MyUserJSONListLength > 0 ->     
            MyFinalList = sensor_data_final(MyUserJSONList),
            io:format("The Present Date Sensor Users Data without Binary 2: ~n~p~n",[MyFinalList]),
            {200, [MyFinalList], Headers};
            %%{json, MyFinalList};
        true ->
            {200, "NO DATA FOUND", Headers}
            %%{json, [{error, "NO DATA FOUND"}]}
        end.        

在 Chicagoboss 服务器日志中,我得到:

The Present Date Sensor Users Data with Binary 1: 
[[<<"{\"username\":\"KPBatman1\",\"building\":\"A\",\"device\":\"Fitbit\",\"date\":\"2017-07-23\",\"calorie\":732,\"distance\":6.4399999999999995,\"elevation\":0,\"floor\":0,\"steps\":8}">>],
 [<<"{\"username\":\"KPSuperman1\",\"building\":\"A\",\"device\":\"Jawbone\",\"date\":\"2017-07-23\",\"calorie\":0,\"distance\":0.0,\"elevation\":0,\"floor\":0,\"steps\":0}">>]]

The Present Date Sensor Users Data without Binary 2: 
[["{\"username\":\"KPBatman1\",\"building\":\"A\",\"device\":\"Fitbit\",\"date\":\"2017-07-23\",\"calorie\":732,\"distance\":6.4399999999999995,\"elevation\":0,\"floor\":0,\"steps\":8}"],
 ["{\"username\":\"KPSuperman1\",\"building\":\"A\",\"device\":\"Jawbone\",\"date\":\"2017-07-23\",\"calorie\":0,\"distance\":0.0,\"elevation\":0,\"floor\":0,\"steps\":0}"]]

但是,当我发送 HTTP 请求时 - 我得到的 JSON 响应:

{"username":"KPBatman1","building":"A","device":"Fitbit","date":"2017-07-23","calorie":732,"distance":6.4399999999999995,"elevation":0,"floor":0,"steps":8}
{"username":"KPSuperman1","building":"A","device":"Jawbone","date":"2017-07-23","calorie":0,"distance":0.0,"elevation":0,"floor":0,"steps":0}

发送 JSON 响应的正确方法是什么?

4

1 回答 1

2

但是,当我发送 HTTP 请求时 - 我得到的 JSON 响应:

{"username":"KPBatman1","building":"A", ...}
{"username":"KPSuperman1","building":"A", ...}

和?你期望/想要得到什么?

以下代码对我有用,因为输出是我期望看到的:

-module(cb_tutorial_greeting_controller, [Req]).
-compile(export_all).

hello('GET', []) ->
    Headers = [
        {'Access-Control-Allow-Origin', "*"},
        {'Access-Control-Allow-Methods',  "GET, OPTIONS"},
        {'Content-Type',  "application/json"},
        {'Access-Control-Allow-Headers', "X-Requested-With"},
        {'Access-Control-Max-Age', "180"}
    ],   
    Data = [
        [<<"{\"username\":\"KPBatman1\",\"building\":\"A\"}">>],
        [<<"{\"username\":\"KPSuperman1\",\"building\":\"A\"}">>]
    ],
    Json = jsx:encode(Data),
    {200, Json, Headers}.

在我的浏览器中,我看到:

[["{\"username\":\"KPBatman1\",\"building\":\"A\"}"],["{\"username\":\"KPSuperman1\",\"building\":\"A\"}"]]

请注意,MyFinalList 甚至不是有效的 JSON:

13> Data = [["{\"a\":\"Batman\"}"], ["{\"b\":\"Superman\"}"]].
[["{\"a\":\"Batman\"}"],["{\"b\":\"Superman\"}"]]

14> jsx:is_json(Data).
false

看看我在那里做了什么?

于 2017-07-24T04:01:42.863 回答