Network request

Does network.request work in html5 builds ?

In simulator it works OK. When I build for html5 and upload files to web server am getting network request failed.

local testNetworkRequest = function () local pingPhpListener = function (event) local properties = "" for k,v in pairs(event) do --print( k,v ) properties = properties .. k .. " = " .. tostring (v) .. "\n" end msgText.text = properties end local pingPhp = network.request ("http://www.google.com", "GET", pingPhpListener) end

With HTML5 builds, you have to follow cross-domain scripting rules. You would need to write a script on your local server that the HTML5 app calls that in turn calls the API you’re trying to call and then pass the results back to your network request.

Rob

Thank you Rob for your answer.

But thats what I did, I have a simple php script on my web server :

\<?php echo "PING OK " . date("Y-m-d-H:i:s"); ?\>

The Html5 app calls this script through network.request

but I am getting network request failed.

Any example from coronalabs will be helpful.

I put the php script in the same directory of the app where index.html is located on the web server. But am getting the same error.

Anyone with a working example ?

Some browsers will also XSS fail if the URL is an IP instead of a domain. Sticking this at the top of your PHP script is usually enough to allow all requests from all sources though:

\<?php header('Access-Control-Allow-Origin: \*'); header('Access-Control-Allow-Headers: \*'); header('Access-Control-Allow-Credentials: true'); ?\>

Thank you very much Richard. It works.

No worries, but do note that this literally allows requests from all sources which somewhat defeats the protection offered by XSS mechanisms. Now that it’s working, you should ideally replace those wildcards with just the sources you actually do want to allow requests from.

With HTML5 builds, you have to follow cross-domain scripting rules. You would need to write a script on your local server that the HTML5 app calls that in turn calls the API you’re trying to call and then pass the results back to your network request.

Rob

Thank you Rob for your answer.

But thats what I did, I have a simple php script on my web server :

\<?php echo "PING OK " . date("Y-m-d-H:i:s"); ?\>

The Html5 app calls this script through network.request

but I am getting network request failed.

Any example from coronalabs will be helpful.

I put the php script in the same directory of the app where index.html is located on the web server. But am getting the same error.

Anyone with a working example ?

Some browsers will also XSS fail if the URL is an IP instead of a domain. Sticking this at the top of your PHP script is usually enough to allow all requests from all sources though:

\<?php header('Access-Control-Allow-Origin: \*'); header('Access-Control-Allow-Headers: \*'); header('Access-Control-Allow-Credentials: true'); ?\>

Thank you very much Richard. It works.

No worries, but do note that this literally allows requests from all sources which somewhat defeats the protection offered by XSS mechanisms. Now that it’s working, you should ideally replace those wildcards with just the sources you actually do want to allow requests from.