How can I return JSONP in RestXQ (using eXist-db)?

255 views Asked by At

I cannot figure out how to return JSONP in RestXQ. After adding let $x := util:declare-option("exist:serialize", fn:concat("method=json jsonp=",request:get-parameter("callback", "callback"))) to the function, I get the error message:

err:XPTY0004:It is a type error if, during the static analysis phase, an expression is found to have a static type that is not appropriate for the context in which the expression occurs, or during the dynamic evaluation phase, the dynamic type of a value does not match a required type as specified by the matching rules in 2.5.4 SequenceType Matching.

The beginning of the GET function is:

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")))
2

There are 2 answers

0
Joe Wicentowski On BEST ANSWER

As discussed on the eXist-open mailing list (I'd suggest joining!), the request module's get-parameter() function is not available inside a RestXQ function. Instead, you can get your callback parameter via the %rest:query-param annotation. Add %rest:query-param("callback", "{$callback}", '') to your contacts:submit() function, and I think you'll be a step closer.

0
adamretter On

@joewiz is correct. Your initial problem is related to the use of eXist request module from RESTXQ, which is unsupported.

Also, RESTXQ does not currently support JSONP serialization. If you want to use JSONP serialization, your best bet at the moment is to manage the serialization to JSON yourself, perhaps using the xqjson library or similar and then wrapping the result in a JSON function using concat or similar.