简体   繁体   English

使用 AJAX 调用 worklight SQL 适配器过程

[英]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?有人可以举例说明如何使用 AJAX 调用在工作灯中调用和接收来自 SQL 适配器的响应吗? I'm using worklight 6.0我正在使用工作灯 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我需要调用一个不需要身份验证的过程(例如:注册帐户),因此需要使用 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
                });

My understanding is that you just want to invoke an Adapter procedure using Ajax from some client.我的理解是您只想从某个客户端使用 Ajax 调用 Adapter 过程。

Is it the same as this other question ?它与另一个问题相同吗?

Calling Worklight adapter from external app 从外部应用程序调用 Worklight 适配器

You can check the details of the HTTP API here您可以在此处查看 HTTP API 的详细信息

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 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.更新:还要注意,如果您配置了任何安全测试,不同于“wl_unprotected”(您的适配器是 100% 公开的,小心!!)您可能需要额外的步骤来处理身份验证。

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.如果你有一个默认的适配器,没有任何安全测试集,你可能会在你的第一个请求中收到一个未经授权的 401,在 401 的正文中你可能会找到一个 WL-Instance-ID 属性,你必须将它与一个新的请求获得使用它的授权。

Update 2: Worklight/MobileFirst Platform doesn't enable CORS (so you can't "naturally" call adapters using ajax from an external web page).更新 2:Worklight/MobileFirst Platform 不启用 CORS(因此您不能“自然地”使用来自外部网页的 ajax 调用适配器)。 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.可以通过使用网关(可能是 IHS)来解决这个问题,该网关将标题“Access-Control-Allow-Origin”添加到所有适配器响应中。 Note that you are handling a security thing, so make sure you know what you are doing.请注意,您正在处理安全问题,因此请确保您知道自己在做什么。

Worklight (6.3 and below) adapters can work only with the Worklight Server. Worklight(6.3 及更低版本)适配器只能与 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.如果您计划使用 Worklight 适配器,则需要使用 Worklight 框架提供的 API - 问题中提到的代码。

You can still use regular AJAX requests - but those won't/cannot be requests sent to/by the Worklight adapter.您仍然可以使用常规 AJAX 请求 - 但这些请求不会/不能是 Worklight 适配器发送到/由 Worklight 适配器发送的请求。

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?如果目标确实需要通过 Worklight Server,并且它不受任何领域的保护,那么有什么问题? 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).也许您不应该在环境级别而是在过程级别保护应用程序(在适配器 XML 中的适配器过程上设置安全测试,而不是在 application-descriptor.xml 中的环境上)。

Perhaps you need to better explain your specific scenario...也许你需要更好地解释你的具体情况......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM