cancel
Showing results for 
Search instead for 
Did you mean: 

Html - XSJS issue

Former Member
0 Kudos

Hi,

I have a xsjs script which delete's a record based on the input parameter. If i execute this via a URL, its deleting the record properly.

But when i call from Html page which has a Text field and a submit button, its not deleting the record even though its calling the xsjs.

------------------------------ XSJS Content --------------------------

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

var body='';

var app_id=$.request.parameters.get('app_id');

var query = "delete from Table where application_id='"+app_id+"'";

var conn = $.db.getConnection();

var pstmt = conn.prepareStatement(query);

var updateCount = pstmt.executeUpdate();

body = updateCount.toString();

conn.commit();

$.response.setBody("Number of records updated:"+body);

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

------------------------------ End  --------------------------

-------------------------- HTML ----------------------

var oButton4 = new sap.ui.commons.Button({

                           text : "Submit",

                           style: sap.ui.commons.ButtonStyle.Emph,

                           press : function(oEvent,oInput1) {

                                       var jURL = '../demo/demo/updateLOG.xsjs';

                                       jQuery.ajax({

                                           url:jURL,

                                           method: 'POST',

                                           dataType:'json',

                                           success: myFunction.onComplete,

                                           error: myFunction.onErrorCall                                                        

                                       });

                              

                                   }

              

              

              

               }).placeAt("content");

---------------------- END --------------------------

Thanks for the help

NAresh

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Thanks for the responses.

Attached is the html.

Its a text field and i would like to do a POST http call. So i am thinking, it should get passed to xsjs in "request" object.

Regards,

Naresh

former_member182862
Active Contributor
0 Kudos

Hi Naresh

I think the ajax call should not be a POST because you are doing

var app_id=$.request.parameters.get('app_id'); 

in XSJS, you would need to do

var body = $.request.body.asString();

if you do a HTTP POST.

Anyway, you can do a HTTP GET to get it to work. Although, I thought HTTP DELETE will be more appropriate

press : function(oEvent,oInput1) {
    var jURL = '../demo/demo/updateTBLOG.xsjs';
    jQuery.ajax({
        url:jURL,
        data: {app_id : $('#app_id').val()},
        type: 'GET',
        success: myFunction.onComplete,
        error: myFunction.onErrorCall
    });
}

Hope this helps

Dennis

Former Member
0 Kudos

Hi Dennis, Thanks for the response. I used GET to make this to work. If i want to use POST, could you tell me how to extract the two fields data in xsjs..after getting var body = $.request.body.asString();? Thanks, Naresh

former_member182862
Active Contributor
0 Kudos

Hi Naresh

If you send over two data fields like this (in HTTP POST)

 
press : function(oEvent,oInput1) {
    var jURL = '../demo/demo/updateTBLOG.xsjs';
    jQuery.ajax({
        url:jURL,
        data: {data1 : 'hello', data2: 'world'},
        type: 'POST',
        success: myFunction.onComplete,
        error: myFunction.onErrorCall
    });
}

Then I believe that var body = $.request.body.asString() will be

data1=hello&data2=world

You have to parse it yourself.

former_member182862
Active Contributor
0 Kudos

And the another thing, that Thomas was pointing at

var query = "delete from Table where application_id='"+app_id+"'";
var conn = $.db.getConnection();
var pstmt = conn.prepareStatement(query);
var updateCount = pstmt.executeUpdate();

can be better written as

var query = "delete from Table where application_id=?";
var conn = $.db.getConnection();
var pstmt = conn.prepareStatement(query);
pstmt.setString(1, app_id);
var updateCount = pstmt.executeUpdate();

Thanks

fabian_krger
Participant
0 Kudos

Did you get this working?

I am trying to do a DELETE but I cant get the conn.prepareStatement() work with the delete query. Selecting is fine, updating is fine, but delete does not work. It just gives me the error-Page of the XS Server.

My HANA Version is Rev 48... if you got it working, which Rev do you use?

former_member182862
Active Contributor
0 Kudos

How does your DELETE Statement looks like?

I will be surprised if DELETE does not work for any revision that you have.

Thanks

Dennis

fabian_krger
Participant
0 Kudos

I guess I need to blame myself while writing this response I found out the user I use for xsengine had only read and update privileges. Now I added delete and it is working. Thanks anyway

henrique_pinto
Active Contributor
0 Kudos

You need to add the URL parameters with the "data" setting of jQuery.ajax() method.

Check the definition here and an example here.

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

I don't see anywhere in your HTML code where you pass the app_id parameter to the service. Shouldn't you be building that into the URL for the AJAX call?

Also please note (although this has nothing to do with your update not working) that you should concatenate variables directly into your SQL statements. This exposes you to the potential for SQL Injection.  You should always use parameterized SQL statements instead.