lure.tuts.xmlhttprequest

Using XMLHttpRequest

Lure has the capability to make HTTP requests. This is accomplished by wrapping the already available socket.http library into a DOM XMLHttpRequest object. Lure's implimentation of XMLHttpRequest supports synchronous and asynchronous HTTP requests.

Shut up and show me how!!

GET Request

function love.load()
	
	--require lure library
	require('lure//lure.lua')

	--create new XMLHttpRequest Object
	http = XMLHttpRequest.new()

	--create a new request, set method, url, and sync option
	http.open("GET", "http://www.love2d.org/", true)
	
	--create callback function to trigger when request readyState changes
	http.onReadyStateChange = function()
		print(http.readyState)
		print(http.status)
		print(http.statusText)
		print(http.responseText)	
	end

	--send your GET request!
	http.send()
end

POST Request

function love.load()
	--require lure library
	require("lure//lure.lua")		

	--create new XMLHttpRequest Object
	http = XMLHttpRequest.new()
	
	--set url, and post params
	url 	= "http://mydomain.com/"
	params 	= "a=1&b=2"
	
	--create a new request, set method, url, and sync option
	http.open("POST", url, true)		

	--set request headers	
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
	http.setRequestHeader("Content-Length", params:len())
	
	--create callback function to trigger when request readyState changes
	http.onReadyStateChange = function()
		print(http.readyState)
		print(http.status)
		print(http.statusText)
		print(http.responseText)		
	end

	--send your POST request!
	http.send(params)
	
end