I have a problem with the rest API.
When i get item states via php “file_get_contents” everything is okay.
But when i want to get the state from various items via an ajax request i get:
“Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.”
well, when using PHP, you can do a simple http get request with the function file_get_contents. there are different solution how to solve http request. one is with curl, one is without curl (e.g .https://stackoverflow.com/a/6609181/7863031). when using php pecl extension you can use functions like http_get, http_post*, http_put* etc.
maybe it is possible allowing the jetty webserver from openhab the origin cross domain request, but currently I dont know how and if.
what you can also do is, to put your html file in the openhab/conf/html directory and do a http request (from your own webserver with a serverside script language) on http://localhost:8080/static/yourHtmlFile.html which executes your ajax
no. I mean an ajax request like you already did (but which didnt work on your own webserver because of corss origin restriction). Put this file into ./conf/static directory (there your file should work!) and then you can do a http request from your own webserver (with a serverside script) to the openhab server (where you placed your file). then you can read the response from your ajax request.
Issue of the topic: I have a frontend application developed in Angular 6 running on my local machine (http://localhost:4200). This frontend calls to OpenHab Runtime (2.3) that it’s running on another machine (http://localhost:3000). When the frontend call to server, it responds with a No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:4200’ is therefore not allowed access. The response had HTTP status code 401. I configure it with org.eclipse.smarthome.cors:enable=true in the services/runtime.cfg.
Sounds like the problem I had before.
I made a workaround with PHP and a curl request.
It looks like the OpenHAB server refuses to answer your request because it won’t accept connection from other origins then localhost. I don’t know the real problem, but a curl request like mentioned above, was working for me.