1. 程式人生 > >微信小程式開發 -- 踩坑

微信小程式開發 -- 踩坑

自學微信小程式,用小程式做了個小專案,踩了一些坑:

坑一:

Navigated to http://1755603039.appservice.open.weixin.qq.com/appservice
VM2507:1 Sat Feb 04 2017 21:46:27 GMT+0800 (中國標準時間) 渲染層錯誤
VM2507:2 SyntaxError: Invalid or unexpected token
(anonymous) @ VM2507:2
VM2509:1 Sat Feb 04 2017 21:46:27 GMT+0800 (中國標準時間) 渲染層錯誤
VM2509:2 webviewScriptError
Invalid or unexpected token
SyntaxError: Invalid or unexpected token
(anonymous) @ VM2509:2
VM2510:1 Sat Feb 04 2017 21:46:27 GMT+0800 (中國標準時間) 渲染層錯誤
VM2510:2 Uncaught SyntaxError: Invalid or unexpected token
(anonymous) @ VM2510:2
VM2511:1 Sat Feb 04 2017 21:46:27 GMT+0800 (中國標準時間) 渲染層錯誤
VM2511:2 ReferenceError: $gwx is not defined
    at 1755603039.debug.open.weixin.qq.com/pages/index/index.html:1438
(anonymous) @ VM2511:2
VM2512:1 Sat Feb 04 2017 21:46:27 GMT+0800 (中國標準時間) 渲染層錯誤
VM2512:2 webviewScriptError
$gwx is not defined
ReferenceError: $gwx is not defined
    at http://1755603039.debug.open.weixin.qq.com/pages/index/index.html:1438:27
(anonymous) @ VM2512:2
VM2513:1 Sat Feb 04 2017 21:46:27 GMT+0800 (中國標準時間) 渲染層錯誤
VM2513:2 Uncaught ReferenceError: $gwx is not defined

出現 $gwx is not defined

提示報錯位置 at http://1755603039.debug.open.weixin.qq.com/pages/index/index.html:1438:27

這個報錯位置提示沒有任何意義,因為index.whtml我好幾天沒有動過了;

這個錯誤後來發現  在剛改過的whtml 頁面 想寫"¥{{user.salary}}/時 "   結果寫成了"${{user.salary}}/時" , 將 ¥ 寫成了$;

坑二

封裝了一下微信提供的網路訪問介面,結果伺服器端死活獲取不到傳遞的引數,最後上網一查,post請求的header必須將content-type設定為 'application/x-www-form-urlencoded',否則後臺就接收不到資料,而我設定的是'application/json' ,沒有其它原因,因為我就是想傳遞json資料啊,另外POST必須要大寫,這點介面文件上寫了,正確程式碼如下:

function request(param) {
     var obj ={
        url: param.url,
        header: {
            'content-type': 'application/x-www-form-urlencoded', /****注意:post方式必須這樣設定,否則後臺獲取不到引數 */
            /***'content-type': 'application/json',**/
        },
        method:'POST',/***注意post必須大寫 */
        data:param.data==undefined ? "":param.data,
        success: function( res ) {
            /**console.log(res.data); */
            if(param.success && typeof(param.success) =="function" ){
                param.success(res);
            }
        },
        fail: function( res ) {
           if(param.fail && typeof(param.fail) =="function" ){
                param.fail(res);
            }
        }
     }
     if(app.globalData.token){
         obj.header.access=app.globalData.token;
     }
     /**
     console.log( obj.url)
     console.log(JSON.stringify(obj.data) );
      */
     wx.request(obj);
     
}