OpenHAB Rest API, No 'Access-Control-Allow-Origin'

Hello All,

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

This is my code:

$.ajax({
	        type: 'GET',
	        url: Link,
	        contenttype: "text/plain",
	        headers: { 'Access-Control-Allow-Origin':'*' },
	        success: function(data) {
	            $(this).html(data);

	            if (data != Old)
					console.log("Updated " + Name + ", from: " + Old + " to " + New);
	        },
		    error: function() { 
	            alert('Failed!');
	        } 
	    });

Someone an idea what is wrong?

Greetings,
Jordo

I believe (but not sure) that the jetty webserver doesnt allow cross origin ajax requests. where is your html file placed? not in openhab/conf/html directory, isnt it?

for simple javascript only things you can put the file in openhab/conf/html. its working from their and you can access it with http://localhost:8080/static/filename.html

if you want to host it from your own webserver, I’ll write my own little webservice in php which is able to access the REST API.

It is placed on my own webserver indeed.
I want to process the data and control some switches and stuff from my own webserver.
Is there an other way to do a ajax request in my internal network?

What kind of service did you wrote?
And can i do that with my own server too?

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

I will give this one a try thank you!

You mean a little javascript what gives the value or change it?
And then from OpenHAB server side?

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.

I think i understand what you mean.
I will give it a try and let you know if it work or not! :slight_smile:

Turns out,
It is posible to send an curl request (POST or PUT) from the webserver to the OpenHab Rest API.
This request will be accepted en processed :slight_smile:

Thanks for help!

  • Platform information:
    • OS: Linux
    • Java Runtime Environment: 1.8
    • openHAB version: 2.3
  • 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.

What’s going wrong?

Thanks

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.