print(xhr.response)
end
– 注册脚本方法回调
xhr:registerScriptHandler(onReadyStateChange)
xhr:send()-- 发送
labelStatusCode:setString(“waiting…”)
end
– 测试Post的标签
local labelPost = cc.Label:createWithTTF(“Test Post”, s_arialPath, 22)
labelPost:setAnchorPoint(cc.p(0.5, 0.5)) – 设置锚点
local itemPost = cc.MenuItemLabel:create(labelPost) – 设置菜单项标签
itemPost:registerScriptTapHandler(onMenuPostClicked) – 注册菜单项点击回调方法
itemPost:setPosition(cc.p(winSize.width / 2, winSize.height - margin - 2 * space))
menuRequest:addChild(itemPost)
–Post Binary
local function onMenuPostBinaryClicked()
local xhr = cc.XMLHttpRequest:new()-- 新建一个XMLHttpRequest对象
xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_ARRAY_BUFFER --返回数据为字节流
xhr:open(“POST”, “http://httpbin.org/post”) – 打开Socket
– 状态改变时调用
local function onReadyStateChange()
local response = xhr.response – 获得返回数据
local size = table.getn(response) – 获得返回数据大小
local strInfo = “”
for i = 1,size do
if 0 == response[i] then
strInfo = strInfo…“‘ ’”
else
strInfo = strInfo…string.char(response[i])
end
end
labelStatusCode:setString(“Http Status Code:”…xhr.statusText)
print(strInfo)
end
– 注册脚本方法回调
xhr:registerScriptHandler(onReadyStateChange)
xhr:send()-- 发送
labelStatusCode:setString(“waiting…”)
end
– 测试使用Post请求方式发送字节流
local labelPostBinary = cc.Label:createWithTTF(“Test Post Binary”, s_arialPath, 22)
labelPostBinary:setAnchorPoint(cc.p(0.5, 0.5))
local itemPostBinary = cc.MenuItemLabel:create(labelPostBinary)
itemPostBinary:registerScriptTapHandler(onMenuPostBinaryClicked)
itemPostBinary:setPosition(cc.p(winSize.width / 2, winSize.height - margin - 3 * space))
menuRequest:addChild(itemPostBinary)
–Post Json
local function onMenuPostJsonClicked()
local xhr = cc.XMLHttpRequest:new() – 新建一个XMLHttpRequest对象
xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_JSON – json数据类型
xhr:open(“POST”, “http://httpbin.org/post”)-- POST方式
local function onReadyStateChange()
– 显示状态码,成功显示200
labelStatusCode:setString(“Http Status Code:”…xhr.statusText)
local response = xhr.response – 获得响应数据
local output = json.decode(response,1) – 解析json数据
table.foreach(output,function(i, v) print (i, v) end)
print(“headers are”)
table.foreach(output.headers,print)
end
– 注册脚本方法回调
xhr:registerScriptHandler(onReadyStateChange)
xhr:send()-- 发送请求
labelStatusCode:setString(“waiting…”)
end
– 测试使用POST方式发送json的标签
local labelPostJson = cc.Label:createWithTTF(“Test Post Json”, s_arialPath, 22)
labelPostJson:setAnchorPoint(cc.p(0.5, 0.5)) – 锚点
local itemPos 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》开源 tJson = cc.MenuItemLabel:create(labelPostJson) – 菜单项标签
itemPostJson:registerScriptTapHandler(onMenuPos Android开源项目《ali1024.coding.net/public/P7/Android/git》 tJsonClicked) – 注册菜单项点击
itemPostJson:setPosition(cc.p(winSize.width / 2, winSize.height - margin - 4 * space))
menuRequest:addChild(itemPostJson)
end
– 节点回调事件
local function onNodeEvent(eventName)
if “enter” == eventName then
init()
end
end
– 注册层的监听回调事件
layer:registerScriptHandler(onNodeEvent)
return layer
end
function XMLHttpRequestTestMain()
local scene = cc.Scene:create() – 创建场景
scene:addChild(XMLHttpRequestLayer()) – 添加层
scene:addChild(CreateBackMenuItem()) – 添加返回菜单项,具体实现查看helper.lua文件
return scene
end
效果图如下:
自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…
uItem()) – 添加返回菜单项,具体实现查看helper.lua文件
return scene
end
效果图如下:
自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…
[外链图片转存中…(img-2YrrlZEK-1650520214844)]



