Server module, changing contenttype

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
User avatar
HMJust
Posts: 51
Joined: Wed May 11, 2016 7:23 pm
Contact:

Server module, changing contenttype

Post by HMJust » Wed Mar 22, 2017 10:43 pm

I have a bit of an issue. I need to return some JSON but because of the cross domain restrictions, I need to send the Access-Control-Allow-Origin header. I have ended up using a server module, like this:

Code: Select all

Begin_Com Role(*EXTENDS #PRIM_SRVM)

Srvroutine Name(LINKS) Response(#RESPONSE)
* #RESPONSE.ContentType := "application/json"
#RESPONSE.AddHeader( "Access-Control-Allow-Origin" "http://x.x.x.x" )
#RESPONSE.ContentString := ...
Endroutine

End_Com
There is of course more code than that :-)

That works fine, and I get something like

Code: Select all

HTTP/1.1 200 OK
Connection: close
Date: Wed, 22 Mar 2017 11:35:37 GMT
Server: Apache
Content-Type: text/plain; charset=iso-8859-1
Access-Control-Allow-Origin: http://x.x.x.x

[{"icon-url": "/images/wamlinks/icons/alien.3.png","link-url": "https://google.com/","title": "Google"}]
back. But the content type is of course wrong. So I try to change this by un-commenting the

Code: Select all

* #RESPONSE.ContentType := "application/json"
line above, and then I get

Code: Select all

HTTP/1.1 200 OK
Connection: close
Date: Wed, 22 Mar 2017 11:36:13 GMT
Server: Apache
Content-Type: application/json
Access-Control-Allow-Origin: http://x.x.x.x

žœ‰ƒ–•`¤™“z@a‰”‡…¢a¦”“‰•’¢a‰ƒ–•¢a“‰…•KóK—•‡k“‰•’`¤™“z@ˆ££—¢zaa‡––‡“…Kƒ–”ak£‰£“…z@Ç––‡“…GŸ
I have tried setting the charset but it did not help. Can anyone tell me what I am doing wrong?
I don't know what I am doing, nor how I did it. If it works, luck was involved :geek:

Post Reply