Html build http.request returns status 0 (SOLVED)

If I do a “Build and launch” with this code on an new project:

function init(self)
    http.request("http://www.google.com", "GET", function(self, id, response)
    	label.set_text("http_test#label", "status: " .. response.status)
    end)
end

I get the status code 302. But if I then do a “Build html and launch” I get status code 0.

I’ve tried on different computers but I still get the same result. I have a feeling I’ve missed something incredibly simple somewhere, like a setting for html builds or something.

Do anyone have an idea why “http.request(…)” doesn’t work for me in html builds but works in normal builds?

Hmm, 302 corresponds to the “Found” http response which is, from what I can read, a kind of redirect. Usually there’s no difference in http.requests if you’re making the request from desktop, browser or mobile. What if you’re making the request to something more reasonable (like https://www.defold.com) that returns a 200?

Could it be due to some CORS restriction? When you do “Build HTML and Launch” the editor will spin up a small webserver running on localhost, which could cause CORS problems if you try to do a HTTP GET of a different domain.

Which web browser are you testing with? Try launching a copy of chrome with the --disable-web-security flag to test if it’s what @sven suggested.

1 Like

Sorry for the late reply, I was sick the last week so I wasn’t that active developing :confused: Anyway, I looked into CORS restriction as @sven said and I found that it was issue, I solved it by configuring the server so that it responded with the headers:

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, PATCH, DELETE
Access-Control-Allow-Headers: X-Requested-With,content-type
Access-Control-Allow-Credentials: true

I also configured the servers so that it handled the OPTIONS method correctly.

After this everything worked perfectly :slight_smile:

4 Likes