Updates XMLHttpRequestTest.lua

This commit is contained in:
James Chen 2015-09-12 19:11:32 +08:00
parent 6073456f23
commit 10e45ee744
1 changed files with 39 additions and 18 deletions

View File

@ -28,17 +28,21 @@ local function XMLHttpRequestLayer()
xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING
xhr:open("GET", "http://httpbin.org/get")
local function onReadyStateChange()
local function onReadyStateChanged()
if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then
local statusString = "Http Status Code:"..xhr.statusText
labelStatusCode:setString(statusString)
print(xhr.response)
if not tolua.isnull(labelStatusCode) then
labelStatusCode:setString("Http Status Code:" .. xhr.statusText)
else
print("ERROR: labelStatusCode is invalid!")
end
else
print("xhr.readyState is:", xhr.readyState, "xhr.status is: ",xhr.status)
end
xhr:unregisterScriptHandler()
end
xhr:registerScriptHandler(onReadyStateChange)
xhr:registerScriptHandler(onReadyStateChanged)
xhr:send()
labelStatusCode:setString("waiting...")
@ -56,17 +60,22 @@ local function XMLHttpRequestLayer()
local xhr = cc.XMLHttpRequest:new()
xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING
xhr:open("POST", "http://httpbin.org/post")
local function onReadyStateChange()
local function onReadyStateChanged()
if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then
labelStatusCode:setString("Http Status Code:"..xhr.statusText)
if not tolua.isnull(labelStatusCode) then
labelStatusCode:setString("Http Status Code:" .. xhr.statusText)
else
print("ERROR: labelStatusCode is invalid!")
end
print(xhr.response)
else
print("xhr.readyState is:", xhr.readyState, "xhr.status is: ",xhr.status)
end
xhr:unregisterScriptHandler()
end
xhr:registerScriptHandler(onReadyStateChange)
xhr:registerScriptHandler(onReadyStateChanged)
xhr:send()
labelStatusCode:setString("waiting...")
end
@ -83,29 +92,36 @@ local function XMLHttpRequestLayer()
xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_ARRAY_BUFFER
xhr:open("POST", "http://httpbin.org/post")
local function onReadyStateChange()
local function onReadyStateChanged()
if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then
local response = xhr.response
local size = table.getn(response)
local strInfo = ""
for i = 1,size do
if 0 == response[i] then
strInfo = strInfo.."\'\\0\'"
else
strInfo = strInfo..string.char(response[i])
end
end
end
labelStatusCode:setString("Http Status Code:"..xhr.statusText)
if not tolua.isnull(labelStatusCode) then
labelStatusCode:setString("Http Status Code:" .. xhr.statusText)
else
print("ERROR: labelStatusCode is invalid!")
end
print(strInfo)
else
print("xhr.readyState is:", xhr.readyState, "xhr.status is: ",xhr.status)
end
xhr:unregisterScriptHandler()
end
xhr:registerScriptHandler(onReadyStateChange)
xhr:registerScriptHandler(onReadyStateChanged)
xhr:send()
labelStatusCode:setString("waiting...")
end
@ -123,9 +139,13 @@ local function XMLHttpRequestLayer()
xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_JSON
xhr:open("POST", "http://httpbin.org/post")
local function onReadyStateChange()
local function onReadyStateChanged()
if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then
labelStatusCode:setString("Http Status Code:"..xhr.statusText)
if not tolua.isnull(labelStatusCode) then
labelStatusCode:setString("Http Status Code:" .. xhr.statusText)
else
print("ERROR: labelStatusCode is invalid!")
end
local response = xhr.response
local output = json.decode(response,1)
table.foreach(output,function(i, v) print (i, v) end)
@ -134,11 +154,12 @@ local function XMLHttpRequestLayer()
else
print("xhr.readyState is:", xhr.readyState, "xhr.status is: ",xhr.status)
end
xhr:unregisterScriptHandler()
end
xhr:registerScriptHandler(onReadyStateChange)
xhr:registerScriptHandler(onReadyStateChanged)
xhr:send()
labelStatusCode:setString("waiting...")
end