cancel
Showing results for 
Search instead for 
Did you mean: 

XSJS Charset problem on SP9

roberto_falk
Participant
0 Kudos

Hi Experts,

I'm executing a simple select inside a xsjs service, and after, returning the result in a json format. All works fine, except that, after SP9 (rev 93) upgrade, the charset is no longer working. See below:

Table PEOPLE:

Name | Surname

João | Silva

Here the XSJS file:

$.response.contentType = 'application/json; charset=utf-8'; 

var conn = $.db.getConnection();

var pstmt = conn.prepareStatement( 'select * from "PEOPLE"' );

var rs = pstmt.executeQuery();

var jsonArr = [];

while (rs.next()){

    jsonArr.push({

        name: rs.getString(1),

        suname: rs.getString(2)

    });

}

rs.close();

pstmt.close();

conn.close();

$.response.setBody(JSON.stringify(jsonArr));

$.response.status = $.net.http.OK;

When running SP8, the result was the following:

[

-{
name: "João",
suname: "Silva"
}

]


Now running SP9, the result is:


[

     -{

name: "João",
suname: "Silva"
}

]


Do you know how can I solve this?


Thanks and regards,

Roberto Falk

Accepted Solutions (1)

Accepted Solutions (1)

thomas_jung
Developer Advocate
Developer Advocate

You are using the wrong get function. 

JSDoc: Class: ResultSet

getString(columnIndex) → {string}

Returns a string value of the specified column. getString is used for CHAR and VARCHAR column types. ASCII only, not suitable for strings containing Unicode characters.

getNString(columnIndex) → {string}

Returns a string value of the specified column. getNString is used for NCHAR, NVARCHAR, SHORTTEXT column types.

I'm not sure why it even worked on SPS 08.  It really shouldn't have.

I might point out that if you are on SPS 09, you can use the new database interface which directly returns JSON and doesn't require the use of any type specific get or set functions.  This would also avoid your problem.

JSDoc: Namespace: hdb

http://scn.sap.com/community/developer-center/hana/blog/2014/12/02/sap-hana-sps-09-new-developer-fea...

roberto_falk
Participant
0 Kudos

Hi Thomas,

I read about this new hdb interface, but in fact the code is much bigger than this example, so, I still need to plan this refactoring.

The funny thing is that, even in the web catalog "/sap/hana/ide/catalog/" this error regarding the charset occurs (now on SP9 - SP8 was also fine).

Anyway, thanks for the fast answer.

Schönes Wochenende.

Roberto Falk

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

Its probably one of those situations where getString wasn't technically working correctly in SPS 08. It was returning a UTF-8 string when it should have been returning an ASCII one. People built upon that bug and were happy. Now the bug is fixed and people are unhappy. That's my guess at least.

lkovacevic
Explorer
0 Kudos

Hi, is it posible that we just got this bug fixed? we were using this $.db.resultSet.getString function and everything was working fine, just now we had to change to getText and foud this post. Needless to say we will work with $.hdb in the future.

We are working con HANA 1.00.122.18.1531375173 since september 2019.

We had to migrate the system to another host last month, I dont know if that could have changed something.

Thanks!

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Roberto,

We found the same problem today, after we updated to SP9.

As we use a library to return all sql select statments ("readSql()"), we just put an javascript function in our .xsjs code:

Ex: decodeURIComponent(escape(rs.getString(i)))

Regards

Caio Melzer