1

I have created a CFFunction that returns JSON from a query, but it is not returning special characters correctly. I have checked the record in the database and it is saving correctly in the database so I know the problem is with the API.

Below is the main of my cffunction:

<cffunction name="getNews" access="remote" httpmethod="GET" returntype="array" produces="application/json>

I have even tried adding charset=utf-8 at the end:

<cffunction name="getNews" access="remote" httpmethod="GET" returntype="array" produces="application/json; charset=utf-8">

but this still does not return the correct characters.

If there is anything else you need let me know, hopefully someone will have come across this issue before.

Miguel-F
  • 13,450
  • 6
  • 38
  • 63
Anthony Doherty
  • 111
  • 1
  • 11
  • 1
    Examples of the output you want and the output being generated would be helpful. What happens if you change the `returntype="array"` to a string `returntype="string"`? – Miguel-F Jun 06 '18 at 12:20
  • ColdFusion produces UTF-8 responses by default. Browsers expect UTF-8 by default. When data appears broken on the screen, then *either* something on the server-side has added a misleading `charset` parameter to the `Content-Type` header, *or* the data was broken at the point the server was reading it. For example, if you read data from files in your ColdFusion code and open them without specifying their encoding. Or when you use an incorrectly configured DB connection. – Tomalak Jun 06 '18 at 13:50

1 Answers1

3

I managed to fix the problem:

  • Log into the CF Admin
  • Under "Server Settings" click "Java and JVM"
  • Under JVM Arguments add: -Dfile.encoding=UTF8
  • Restart the server and the page will display unicode.

I didn't have to change any other details in my API. Once I restarted the server the JSON came through correctly.

Community
  • 1
  • 1
Anthony Doherty
  • 111
  • 1
  • 11