1

http://ocsigen.org/eliom/manual/server-params#h5o-3显示了一个接受用户定义类型的 GET 服务示例。我想从客户端调用具有 user_type 的协同服务,在客户端使用相同的类型。似乎应该是可能的,但我明白了

ocsigenserver: main: Exn during page generation: Failure("User service parameters 'foo' type not supported client side.") (sending 500)

当我尝试

{shared{
  open Eliom_lib
  open Eliom_content
  open Html5.D

  type foo = A | B
  let foo_of_string = function "a" -> A | "b" -> B | _ -> raise (Failure "foo_of_string: unsupported foo")
  let string_of_foo = function A -> "a" | B -> "b"
}}

let foo_to_div foo : Html5_types.div Html5.elt Lwt.t = match foo with A -> Lwt.return (div [pcdata "aye"]) | B -> Lwt.return (div [pcdata "bee"])
let foo_service =
  Eliom_registration.Ocaml.register_post_coservice'
    ~post_params:Eliom_parameter.(user_type foo_of_string string_of_foo "foo")
    (fun () foo -> foo_to_div foo)

{client{
  let test () =
    Eliom_client.call_ocaml_service ~service:%foo_service () A
 }}


(* Boilerplate from eliom-distillery: *)
module Testing_app = Eliom_registration.App
  (struct let application_name = "testing" end)
let main_service =
  Eliom_service.App.service ~path:[] ~get_params:Eliom_parameter.unit ()
let () =
  Testing_app.register ~service:main_service
    (fun () () -> Lwt.return (Eliom_tools.F.html ~title:"foo"
                                Html5.F.(body [ pcdata "bar" ])))

我也尝试使用server_function,但后来我遇到了如何将 div 作为 json 返回的问题;正在做

type div_elt = Html5_types.div Eliom_content.Html5.elt
let div_elt_json = Json.t<div_elt>

给我Error: Unbound module Html5_types.Json_div

4

1 回答 1

1

偶然我在阅读服务器功能后向下滚动了一下,发现http://ocsigen.org/eliom/4.1/manual/clientserver-communication#h5o-5说要改为

{shared{ 
  type foo = A | B deriving (Json)
  type foo_json = Json.t<foo>
}}
let foo_service =
  Eliom_registration.Ocaml.register_post_coservice'
    ~post_params:Eliom_parameter.(ocaml "param" foo_json)
    (fun () foo -> foo_to_div foo)

这有效=D

(虽然我仍然更喜欢使用 server_function,这似乎更简洁一些。)

于 2014-11-21T11:12:37.537 回答