Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Network request
Started by Lourenco Jun 09 2019 04:03 AM

- - - - -
6 replies to this topic

Best Answer richard11 , 11 June 2019 - 11:35 AM

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');
?>

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Lourenco

[GLOBAL: userInfoPane.html]
Lourenco
  • Contributor

  • 174 posts
  • Corona SDK

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



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,896 posts
  • Enterprise

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



[TOPIC: post.html]
#3

Lourenco

[GLOBAL: userInfoPane.html]
Lourenco
  • Contributor

  • 174 posts
  • Corona SDK

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.



[TOPIC: post.html]
#4

Lourenco

[GLOBAL: userInfoPane.html]
Lourenco
  • Contributor

  • 174 posts
  • Corona SDK

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 ?



[TOPIC: post.html]
#5

richard11

[GLOBAL: userInfoPane.html]
richard11
  • Contributor

  • 448 posts
  • Corona SDK

  Best Answer

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');
?>

  • Lourenco likes this

[TOPIC: post.html]
#6

Lourenco

[GLOBAL: userInfoPane.html]
Lourenco
  • Contributor

  • 174 posts
  • Corona SDK

Thank you very much Richard. It works.



[TOPIC: post.html]
#7

richard11

[GLOBAL: userInfoPane.html]
richard11
  • Contributor

  • 448 posts
  • Corona SDK

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.


[topic_controls]
[/topic_controls]