Get, Post get JSON data for Weex stream fetch

Source: Internet
Author: User

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&param2=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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.