ajaxibm-mobilefirstworklight-adapters

Invoke worklight SQL adapter procedure using AJAX


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
                });

Solution

  • 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.