weex stream 之fetch的get、post擷取Json資料

來源:互聯網
上載者:User

標籤:demo   請求   uri   stream   簡單   href   result   bsp   color   

無論何種平台,網路資料的擷取都是十分重要的,最近學習weex,不可避免的要學習weex的資料要求方法了。網址

個人感覺,weex stream相較於其他平台,還算比較簡單了,但是由於文檔以及官方代碼中的錯誤,導致網路請求很難擷取到自己想要的資料,再次簡單記錄一下遇到的坑。

 

 

一、擷取modal,stream,config對象

var modal = weex.requireModule(‘modal‘);var stream = weex.requireModule(‘stream‘);var config = require(‘./config.js‘)

 

二、get請求

get請求還好,按照官方的demo寫就沒什麼問題了。

clickTypeGet:function(){                                var me = 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‘: "失敗",                            ‘duration‘: 2.0                        })                    }else{                        console.log(‘get---------:‘+JSON.stringify(ret));                        me.getResult = JSON.stringify(ret);                        modal.toast({                            message: JSON.stringify(ret),                            duration: 2.0                        })                    }                })            },

三、post請求

clickTypePost:function(){                var me = 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‘: "失敗",                            ‘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);                })            }

這裡的body不能像官方一樣寫,官方是這麼寫的:
事實證明,這麼寫,始終擷取不到資料,也會提示資料請求成功,但是想要的資料卻始終沒有

                    body:JSON.stringify({                        type:‘shentong‘,                        postid:‘3333557693903‘,                    }),

經過幾番亂試,終於發現,只是因為body寫法不對,造成的post請求擷取不到資料,我們是這麼寫的

 

body:config.toParams(                            {                                type:‘shentong‘,                                postid:‘3333557693903‘,                            })

其中的toParams是自己寫的一個工具方法:

toParams(obj) {        var param = ""        for(const name in obj) {            if(typeof obj[name] != ‘function‘) {                param += "&" + name + "=" + encodeURI(obj[name])            }        }        return param.substring(1)    },

 

小結:其實body字串的格式是‘param1=p1&param2=p2’。



注意:fetch請求在電腦端瀏覽器會被提醒跨域,請求被攔截,直接用手機測試

weex stream 之fetch的get、post擷取Json資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.