jqueryhanaxsjs

Cannot pass characters + or % using xsjs / jQuery.get


I am making a HANA application in SAP HANA Studio. I am able to insert entries into a hdbtable that I made when they are normal things like "1" and "apple". When I try to add values such as "+" or "%" for some reason they show up as a space in my data preview. How come they aren't be stored? How can I fix this so I can add these characters? Changing NVARCHAR and VARCHAR doesn't work.

Below is my schema for my table.

table.schemaName = "scheming";
table.tableType = COLUMNSTORE;
table.description = "test table";
table.columns = [
    {name = "id"; sqlType = NVARCHAR; nullable = false; length = 10; comment = "id"; },
    {name = "desc"; sqlType = VARCHAR; nullable = false; length = 10; comment = "desc";}
];

table.primaryKey.pkcolumns = ["id"];

and here's my xsjs file:

$.response.contentType = "text/html";

var id = $.request.parameters.get('id');
var desc = $.request.parameters.get('desc'); 

$.trace.debug("Here is my log.");

try {
    var conn = $.db.getConnection();

    var st = conn.prepareStatement("INSERT INTO \"scheming\".\"blah.blah::test_table\" values(?,?)");

    st.setString(1, id);
    st.setString(2, desc);

    st.execute();

    st.close();
    conn.commit();
    conn.close();

    $.response.setBody('X');

} catch (err) {
    $.response.setBody(err);
    // $.response.status = $.net.http.INTERNAL_SERVER_ERROR
}

controller file

jQuery(document).ready(
                function() {
                    jQuery.get("/com/colpal/training/test1/Insert.xsjs?id=" + id +"&desc=" + desc,
                            function(result) {

                        if (result == 'X'){
                            console.log("Inserted");
                        }else if(result == 'Y'){
                            console.log("Fail");
                        }

                    });
                });

Solution

  • The snipped of your controller shows that you are passing over id and desc without encoding in the URL. That does not work for characters that have a special meaning in the URL like + and %. You can find details about URL encoding and reserved characters in RFC 3986.

    Use JavaScript's encodeURIComponent to encode the individual data when constructing the URL:

    jQuery.get("/com/colpal/training/test1/Insert.xsjs?id=" +
        encodeURIComponent(id) + "&desc=" + encodeURIComponent(desc), ...