#1 2017-04-14 16:55:15

soeren.baek
Member
Registered: 2017-04-14
Posts: 4

Calling REST service returning JSON

Hi,

I am currently evaluating your software and have some problems connecting to our services.
By using the Sample Database project with the link: https://www.davidesperalta.com/Humm/Sit … wNotes.php
the service returns without any problems, but using any other service, the following message comes:
Error while retrieving notes. Can't connect to the server.

The server has no authentication activated and the target file: (http://localhost/result.json) contains the same information as from within the ShowNotes.php

What am i doing wrong?

Kind Regards
Soeren Baek

Offline

#2 2017-04-14 16:59:56

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,661
Website

Re: Calling REST service returning JSON

Hello Soeren,

Thanks for your interest in my software. Probably you are dealing with a "CORS" permissions problem, that is, your PHP script must enable the "CORS" permissions to allow the application to request it. Try to place the below code at the beginning of your PHP script:

// Enable CORS (http://enable-cors.org/server_php.html)
header('Access-Control-Allow-Origin: *');
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
    header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
  exit;
}

P.S. You can find the above code in the "Initialize.php" script of the Database application sample.

P.S.2. Certainly the App Builder debugger is not too good with this kind of error: try to debug your application in Firefox, for example, and be sure you view the Developer Console by press F12 key once your application is loaded. Probably you can see information about the "CORS" problem in the Developer Console.

Offline

#3 2017-04-14 17:27:16

soeren.baek
Member
Registered: 2017-04-14
Posts: 4

Re: Calling REST service returning JSON

Hi David,
I am not sure if i understood this correctly.
I am not using PHP on the web server, but a simple JSON file, is this my problem?
Do I need to wrap something around the content?

Thank you for your support.
Soeren Baek

Offline

#4 2017-04-14 17:53:47

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,661
Website

Re: Calling REST service returning JSON

Hello Soeren,

Yes; When we want to access a file in a remote server from our applications, the appropiate "CORS" permissions on that server must be enabled in order to reach the required file. If we deal with a PHP script, then we can use PHP code in order to enable the "CORS" permissions directly for that PHP script, in other case (like for a JSON file) we must enable the "CORS" permissions anyway.

If our server support PHP then we can prepare a PHP script to serve the JSON file contents in this way:

<?php

// Enable CORS (http://enable-cors.org/server_php.html)
header('Access-Control-Allow-Origin: *');
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
    header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
  exit;
}

// Write out the JSON file

header('Content-Type: application/json');

echo file_get_contents('my-file.json');

Note in this case the "my-file.json" file is placed in the same directory than the PHP script. Then what we must to do in our application is to use the URL for the PHP script instead of the JSON file. Please, tell me if you have any further question Soeren.

Offline

#5 2017-04-14 20:04:52

soeren.baek
Member
Registered: 2017-04-14
Posts: 4

Re: Calling REST service returning JSON

Hallo David,
It is quite strange.
I have managed to activate CORS in the ISS including changing the Handle module to ISAPI.
But, I still have problems:

:9999/#!/NotesView:1 XMLHttpRequest cannot load http://localhost/result.json. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '[url]http://127.0.0.1:9999[/url]' is therefore not allowed access. The response had HTTP status code 503.

I have added the following HTTP Headers:

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS
Access-Control-Allow-Origin: *

Do you have any idear what could be the issue?

Many Thanks
Soeren Baek

Offline

#6 2017-04-14 20:09:19

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,661
Website

Re: Calling REST service returning JSON

Hello Soeren,

Unfortunately I have no experience on ISS, so I only can point you to this article that hope can help you. I can see you add the "Access-Control-Allow-Origin" header, and the error complaints about this particular header, however, maybe you are not adding such header in the right way? The error is more or less clear... we doesn't have access to that file... due to the "CORS" permissions... so we must find the proper solution for this on an ISS server.

I am sorry if can't help you too much this time... maybe another mate have experience on that server and this issue.

Offline

#7 2017-04-15 18:52:04

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,661
Website

Re: Calling REST service returning JSON

Hello Soeren,

Finally you got it? If so, please, share with us how you configure the ISS server!

Offline

#8 2017-04-18 14:28:23

soeren.baek
Member
Registered: 2017-04-14
Posts: 4

Re: Calling REST service returning JSON

Hi David,

I have another question regarding your product: Is it possible to call web service SOAP?

Best Regards
Soeren Baek

Offline

#9 2017-04-18 14:34:36

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,661
Website

Re: Calling REST service returning JSON

Hello Soeren,

soeren.baek wrote:

Hi David,

I have another question regarding your product: Is it possible to call web service SOAP?

Best Regards
Soeren Baek

Maybe you can prepare a "bridge" in your server in order to consume SOAP services in a more or less easy way. From App Builder you probably can also do it, but not out of the box, that is, you need to prepare the appropiate Javascript code to prepare the appropiate XML, etc. In the first case your application can communicate with your server via HTTP calls, and then your server can consume the SOAP service and provide the appropiate response for the application.

Offline

Board footer

Powered by FluxBB


Copyright ©2018 DecSoft. All rights reserved.