1
votes

Can someone give an example of how to invoke and receive a response from an SQL adapter in worklight using AJAX call? I'm using worklight 6.0

I have security at the app level. I need to invoke a procedure that doesn't need authentication (example: Registering an account) and hence need to invoke the adapter using AJAX

This is my adapter:

var invocationData = {
                    adapter : 'UserInfo',
                    procedure : 'addUserInfo',
                    parameters : [ customerData.firstName,
                            customerData.lastName, customerData.email,
                            customerData.province, customerData.zipPostal,
                            customerData.phoneNumber, customerData.streetName,
                            customerData.streetNumber, customerData.country,
                            customerData.city ]
                };
                WL.Client.invokeProcedure(invocationData, {
                    onSuccess : insertUserSuccess,
                    onFailure : insertUserFailure
                });
2

2 Answers

2
votes

My understanding is that you just want to invoke an Adapter procedure using Ajax from some client.

Is it the same as this other question ?

Calling Worklight adapter from external app

You can check the details of the HTTP API here

http://www-01.ibm.com/support/knowledgecenter/SSZH4A_6.0.0/com.ibm.worklight.help.doc/admin/r_http_interface_of_the_prod_server.html?lang=en

Update: Also note that if you have any security tests configured, different than "wl_unprotected" (your adapter is 100% public with that, be careful!!) you may need extra steps to handle authentication.

If you have a default adapter, without any security test set, you may receive a 401 unauthorized in your first request, and in the body of the 401 you may find a WL-Instance-ID property, that you must send together with a new request to get authorized to use it.

Update 2: Worklight/MobileFirst Platform doesn't enable CORS (so you can't "naturally" call adapters using ajax from an external web page). It may be possible to workaround that by using a gateway (IHS maybe) that adds the header "Access-Control-Allow-Origin" to all adapter responses. Note that you are handling a security thing, so make sure you know what you are doing.

1
votes

Worklight (6.3 and below) adapters can work only with the Worklight Server. If you plan on using Worklight adapters, you are required to use the API as provided by the Worklight framework - the code mentioned in the question.

You can still use regular AJAX requests - but those won't/cannot be requests sent to/by the Worklight adapter.

WLJQ.ajax( "some-URL" )
.done(function (data) {
    console.log(data);
});

If the destination does require going through the Worklight Server, and it is not protected by any realm, what is the problem then? Send the request.

Perhaps you should not protect the app at the environment level but rather at the procedure level (set the security test on the adapter procedure in the adapter XML and not on the environment in application-descriptor.xml).

Perhaps you need to better explain your specific scenario...