Regardless of the platform, the access to the network data is very important, recently learning Weex, it is inevitable to learn the Weex data request method. Url
Personally, Weex stream is relatively simple compared to other platforms, but due to errors in the documentation and official code, it is difficult to get the data you want, and once again simply record the pits.
First, Get Modal,stream,config object
var modal = Weex.requiremodule ('modal'); var stream = Weex.requiremodule ('stream'); var config = require ('./config.js')
Second, GET request
Get request is OK, according to the official demo write there is no problem.
clicktypeget:function () {varme = This; Stream.fetch ({method:'GET', type:'JSON', URL:'Https://api.github.com/repos/alibaba/weex'}, function (ret) {if(!Ret.ok) {Me.getresult="request failed"; Modal.toast ({'message':"failed", 'Duration':2.0 }) }Else{Console.log ('Get---------:'+json.stringify (ret)); Me.getresult=json.stringify (ret); Modal.toast ({message:JSON.stringify (ret), Duration:2.0 }) } }) },
Third, POST request
clicktypepost:function () {varme = This; Stream.fetch ({method:"POST", type:'JSON', URL:'Http://www.kuaidi100.com/query', headers:{'Content-type':'application/x-www-form-urlencoded'},//body: ' type=shentong&postid=3333557693903 ',body:config.toParams ({type:'Shentong', PostID:'3333557693903', })////body:JSON.stringify ({////type: ' Shentong ',//PostID: ' 3333557693903 ',// }),}, function (ret) {if(!Ret.ok) {Me.getresult="request failed"; Modal.toast ({'message':"failed", 'Duration':2.0 }) }Else{Console.log ('Get---------:'+json.stringify (ret.data)); Me.getresult=json.stringify (ret); Modal.toast ({message:JSON.stringify (Ret.data), Duration:2.0})}},function (progress) {//json.stringify (progress.length); }) }
The body here cannot be written like an official, as the official wrote:
As it turns out, the data is not always available and the data request succeeds, but the desired data is never
body:JSON.stringify ({ type: ' Shentong ', postid: ' 3333557693903 ', }),
After a few attempts, finally found, just because the body is wrong, resulting in a POST request not to get data, we wrote this
Body:config.toParams ( { type:'shentong', postid:' 3333557693903', })
One of the toparams is a tool method that you write yourself:
toparams (obj) {varparam ="" for(ConstNameinchobj) { if(typeofObj[name]! ='function') {param+="&"+ name +"="+encodeURI (Obj[name])}} returnParam.substring (1) },
Summary: Actually the format of the body string is ' param1=p1¶m2=p2 '.
Note: The fetch request will be alerted across domains in the computer-side browser, the request is intercepted, and the phone is tested directly
Get, Post get JSON data for Weex stream fetch