RESTful API - Cannot get POST requests to work

Hi, I am trying to trigger a light turning on from my locally hosted webpage (same ip address that home assistant is hosted on - also listed as a trusted website in the configuration.yaml file), but every POST request fails and returns

XMLHttpRequest cannot load http://ip-address:8123/api/service/light/turn_on. Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://ip-address’ is therefore not allowed access. The response had HTTP status code 404.

I am doing the following to do an ajax POST request:

var data={ "entity_id": "light.hue_white_lamp_1"};
$.ajax({
                  url: 'http://'+document.URL.split("/")[2]+':8123/api/service/light/turn_on',
		  dataType:"json",
                  data:JSON.stringify(data),
                  contentType:'application/json; charset=utf-8',
                  type: 'POST',
                  success: function(res) {
                    console.log(res);
                  }
               });

I have used ajax GET requests to get the current state of entities, but I have been unable to get the POST requests to work properly. Any help would be appreciated

can you do the request from a browser window? It sounds like it’s some type of authentication issue getting to the API

Yes, I’m running the Ajax call on a button click on my webpage and have tried to run the post request in the console. It works perfectly fine for get requests, just throws that error when I try to do a post request.

Haha it’s services, not service in the URL. Stupid mistake…

My middle son (21 years old) wandered through last night while I was (let’s just say expressing my displeasure at my computer and python in general). He laughed and said “Dad, it’s the little things you need to look for. It’s something simple. it always is”. About 10 minutes later I found where I had left out a colon at the end of an if statement. It happens to us all. :slight_smile: