如何在 RestXQ 中返回 JSONP(使用 eXist-db)



我不知道如何在RestXQ中返回JSONP。添加后 let $x := util:declare-option("exist:serialize", fn:concat("method=json jsonp=",request:get-parameter("callback", "callback")))到函数,我收到错误消息:

err:XPTY0004:如果在静态分析阶段发现表达式的静态类型不适合表达式发生的上下文,或者在动态计算阶段,值的动态类型与 2.5.4 序列类型匹配中的匹配规则指定的所需类型不匹配,则属于类型错误。

GET 函数的开头是:

declare
    %rest:GET
    %rest:path("/demo/contacts/submit")
    %rest:query-param("email", "{$email}", '')     
    %rest:query-param("nomail", "{$nomail}", 0)    
    %rest:produces("application/javascript")
    %output:media-type("application/javascript")
    %output:method("json")
function contacts:submit($email as xs:string*, $nomail as xs:integer*)
{

    try
    {
        let $x := util:declare-option("exist:serialize", fn:concat("method=json jsonp=",request:get-parameter("callback", "callback")))

正如在 eXist-open 邮件列表中所讨论的(我建议加入!),请求模块的get-parameter()函数在 RestXQ 函数中不可用。相反,您可以通过%rest:query-param注释获取callback参数。 将%rest:query-param("callback", "{$callback}", '')添加到您的contacts:submit()功能中,我认为您会更近一步。

@joewiz是正确的。您的初始问题与使用 RESTXQ 的 eXist 请求模块有关,该模块不受支持。

此外,RESTXQ 目前不支持 JSONP 序列化。如果你想使用 JSONP 序列化,目前最好的办法是自己管理对 JSON 的序列化,也许使用 xqjson 库或类似的东西,然后使用 concat 或类似的方法将结果包装在 JSON 函数中。

最新更新