Javascript calling Turnkey rest
No edit summary
(Automatically adding template at the end of the page.)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
If your javascript is on the same domain there is no issue to call viewmodels exposed with Rest. But if it is from a cross domain your turnkey server needs to enable [[cors]] for the particular domains you will be calling from.  
If your Javascript is on the same domain, there is no issue to call the ViewModels exposed with Rest. If it is from a cross-domain, your Turnkey server needs to enable [[cors]] for the particular domains you will be calling from.


We cannot allow for every domain since that will not work with login credentials (web standards).
We cannot allow every domain since that will not work with login credentials (web standards).  


To set allowed domains in TurnkeyCore there is a new setting in app_data/TurnkeySettings.xml file:
To set allowed domains in TurnkeyCore, follow the model pattern described here: [[Cors]].
<CorsWithOrigins><nowiki>https://localhost:44387,https://localhost:1212</nowiki></CorsWithOrigins>
To allow for specific domains in IIS Turnkey you need to fix the config file for the app, or the config file above the app as described in this article [[Cors]].


ViewModels with RestAllowed will check access groups - and most likely you will use the IsLoggedIn access group - and have SysSingleton.oclSingleton.CurrentUser in your ViewModel to access user specific data. In order for this to work you must ensure that the user is first logged into Turnkey
ViewModels with RestAllowed will check access groups - and most likely, use the IsLoggedIn access group - and have SysSingleton.oclSingleton.CurrentUser in your ViewModel to access user-specific data. For this to work, ensure that the user is first logged into Turnkey.


We suggest one of the following scenarios to ensure user login:
We suggest one of the following scenarios to ensure user login:
# You put the url to your app on a turnkey page - and only show the url if the user is logged  
# Put the URL to your app on a Turnkey page - and only show the URL if the user is logged in
# You send the user from your app to the Turnkey app and have a link on that page to redirect once logged in
# Send the user from your app to the Turnkey app and have a link on that page to redirect once logged in
# You have a Turnkey RestEnabled ViewModel without access group that can answer if the user is loggen in or not - so that you know if to send user to login pages
# Have a Turnkey RestEnabled ViewModel without an access group that can answer if the user is logged in or not - and know whether to send the user to login pages or not


You may now call Turnkey from a valid origin with code like this:
You may now call Turnkey from a valid origin with code like this:
Line 40: Line 38:
         }
         }
     </script>
     </script>
}</pre>If the TurnkeyServer have cors error it will just say "error", if the requested ViewModel has been blocked by AccessControlGroup it will say AccessDenied
}</pre>If the TurnkeyServer has a Cors error, it will just say "error". If the requested ViewModel has been blocked by AccessControlGroup, it will say AccessDenied.


Data returned is Json.
The returned data is Json.


The xhrFields: { withCredentials: true } is important to reuse the session coookie from the user login.
The xhrFields: { withCredentials: true } is important to reuse the session cookie from the user login.
[[Category:MDriven Turnkey]]
[[Category:View Model]]
{{Edited|July|12|2024}}

Latest revision as of 15:35, 10 February 2024

If your Javascript is on the same domain, there is no issue to call the ViewModels exposed with Rest. If it is from a cross-domain, your Turnkey server needs to enable cors for the particular domains you will be calling from.

We cannot allow every domain since that will not work with login credentials (web standards).

To set allowed domains in TurnkeyCore, follow the model pattern described here: Cors.

ViewModels with RestAllowed will check access groups - and most likely, use the IsLoggedIn access group - and have SysSingleton.oclSingleton.CurrentUser in your ViewModel to access user-specific data. For this to work, ensure that the user is first logged into Turnkey.

We suggest one of the following scenarios to ensure user login:

  1. Put the URL to your app on a Turnkey page - and only show the URL if the user is logged in
  2. Send the user from your app to the Turnkey app and have a link on that page to redirect once logged in
  3. Have a Turnkey RestEnabled ViewModel without an access group that can answer if the user is logged in or not - and know whether to send the user to login pages or not

You may now call Turnkey from a valid origin with code like this:

<div>
    <input type="button" value="Try it" onclick="sendRequest()" />
    <span id='value1'>(Result)</span>
</div>

@section scripts {
    <script>
      var serviceUrl = 'http://localhost:5052/TurnkeyRest/Get?command=ViewModel1';
      function sendRequest() {

        $.ajax({
            type: "get",
            url: serviceUrl,
            xhrFields: { withCredentials: true }
        }).done(function (data) {
            debugger;
            $('#value1').text(data);
        }).fail(function (jqXHR, textStatus, errorThrown) {
            debugger;
            $('#value1').text(jqXHR.responseText || textStatus);
        });
        }
    </script>
}

If the TurnkeyServer has a Cors error, it will just say "error". If the requested ViewModel has been blocked by AccessControlGroup, it will say AccessDenied.

The returned data is Json.

The xhrFields: { withCredentials: true } is important to reuse the session cookie from the user login.

This page was edited 88 days ago on 02/10/2024. What links here