HTTP request - problem with response under HTML version ( SOLVED)

I use below code:

  local function handle_response(self, id, response)
	request = response.response   <--- response from my serwer WWW/PHP
end

local headers = {
	["Content-Type"] = "application/x-www-form-urlencoded"
}
local body = "score=" .. self.score;
http.request("https:.......   save.php", "POST", handle_response, headers, body)

The windows EXE version and the version in the console
return me a value from the server. However, I am not getting the value in the HTML version.

Under HTML5 version I get response.status = 0 and in chrom console I get
Access to XMLHttpRequest at ‘https://… save.php’ from origin ‘http://localhost:60710
has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

What can I do ?

Problem solved

I need to create a file .htaccess

Header set Access-Control-Allow-Origin "*"

Some more insight on what’s happening:

As a security measure, your browser generally does not allow outgoing HTTP requests to be made from a page to another domain. Imagine for example if you visit my page, and the javascript I’ve put there uses your browser and your cookies to make a request to your bank’s website.

In the case of GET requests the request gets sent but the browser does not return the response to the javascript code. In the case of a POST request a special OPTIONS request is made first to check the CORS policy of the web browser, and if a request is not allowed from the domain you’re on, the
request is blocked entirely.

This is an ok solution, but it may not allow requests with cookies if I remember correctly. Generally there you want to use the specific domains to allow instead of a wildcard, so in your case localhost:60710.

4 Likes