cocos2dx lua http请求获取网络数据:
2016-10-14 11:22
567 查看
cocos2dx lua http请求获取网络数据:
注:
请求状态 xhr.readyState == 4
数据返回格式 xhr.responseType
cc.XMLHTTPREQUEST_RESPONSE_STRING = 0 -- 返回字符串类型
cc.XMLHTTPREQUEST_RESPONSE_ARRAY_BUFFER = 1 -- 返回字节数组类型
cc.XMLHTTPREQUEST_RESPONSE_BLOB = 2 -- 返回二进制大对象类型
cc.XMLHTTPREQUEST_RESPONSE_DOCUMENT = 3 -- 返回文档对象类型
cc.XMLHTTPREQUEST_RESPONSE_JSON = 4 -- 返回JSON数据类型
local xhr = cc.XMLHttpRequest:new() --创建一个请求 xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING --设置返回数据格式为字符串 local req = "http://www.XXX” --请求地址 xhr:open("GET", req) --设置请求方式 GET 或者 POST local function onReadyStateChange() --请求响应函数 if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then —请求状态已完并且请求已成功 local statusString = "Http Status Code:"..xhr.statusText print("请求返回状态码"..statusString) local s = xhr.response —获得返回的内容 print(“返回的数据") end end xhr:registerScriptHandler(onReadyStateChange) --注册请求响应函数 xhr:send() —最后发送请求
注:
请求状态 xhr.readyState == 4
0: 请求未初始化 1: 服务器连接已建立 2: 请求已接收 3: 请求处理中 4: 请求已完成,且响应已就绪 |
cc.XMLHTTPREQUEST_RESPONSE_STRING = 0 -- 返回字符串类型
cc.XMLHTTPREQUEST_RESPONSE_ARRAY_BUFFER = 1 -- 返回字节数组类型
cc.XMLHTTPREQUEST_RESPONSE_BLOB = 2 -- 返回二进制大对象类型
cc.XMLHTTPREQUEST_RESPONSE_DOCUMENT = 3 -- 返回文档对象类型
cc.XMLHTTPREQUEST_RESPONSE_JSON = 4 -- 返回JSON数据类型
相关文章推荐
- cocos2dx lua http请求获取网络数据:
- 第十二篇:Android GET请求获取网络数据以及POST数据(使用HttpURLConnection)
- HttpURLConnection获取网络数据(实例:网路请求图片)
- 通过网卡获取http请求数据
- android网络数据请求之HttpClient
- Android HttpClient多线程获取网络数据抛出ConnectionPoolTimeoutException: Timeout waiting for connection
- iOS网络编程-ASIHTTPRequest小例子-数据请求队列
- android通过httpClient请求获取JSON数据并且解析
- 网络数据请求NSMutableURLRequest-NSHTTPURLResponse
- android通过httpClient请求获取JSON数据并且解析
- WebClient HttpWebRequest从网页中获取请求数据
- Android-获取网络数据、发送请求
- 【COCOS2DX-LUA 脚本开发之八】不用CCHttpClient而转用quick-cocos2d-x或cocos2dx-LuaProxy的Lua框架实现Lua-Http网络交互!
- Android 如何通过HTTP获取网络上的数据?
- android获取URLConnection和HttpClient网络请求响应码
- Silverlight——Silverlight请求HttpHandler获取数据
- C# Get请求数据 WebClient和HttpWebRequest获取HTML代码 蓝色梦想网
- iOS网络编程-ASIHTTPRequest小例子-数据请求队列
- iOS网络编程-ASIHTTPRequest小例子-数据请求队列
- Android使用HTTP GET、HTTP POST获取网络数据