zoukankan      html  css  js  c++  java
  • 微信小程序request请求正确写法

      微信小程序开发中网络请求必不可少.GET .POST请求是最常用的.GET请求,POST请求的时候有好几个坑.我已经为大家填好了.
            之前的.GET 请求都是如下这样写:(给大家举个例子)

    wx.request({
    url: url,
    data: {
    teacherid: teacherid
    },
    header: {
    'content-type': 'application/json'
    },
    success: (res) => {
    // console.log(res.data);
    this.setData({
    testpaper: res.data.testpaper,
    teacher : res.data.teacher
    });
    }
    })


    但是post方式提交的话就有所改变了,给大家列出以下几点注意事项:

    1.  'Content-Type': 'application/json'用在get请求中没问题.

    POST请求就不好使了.需要改成 :  "Content-Type": "application/x-www-form-urlencoded"

    2. 加上 method: "POST"

    3. data: { answer : { "a":10,"b":8,"c":6 } } 写成json格式这样也是请求不到数据的.需要转格式.

    这里我用JSON.Stringify() 将json对象转换成json字符串格式

    部分代码分享给大家,这里answer与student 都是json对象格式需要转换

    wx.request({
    url : "https://www.",
    method: "POST",
    data: {
    answer : JSON.stringify(this.data.answer),
    score : _score,
    pjid : this.data.pj.pjid,
    testpaperid : this.data.pj.testpaperid,
    student : JSON.stringify(this.data.student),
    message : this.data.message
    },
    header: {
    "Content-Type": "application/x-www-form-urlencoded"
    },
    success: function (res) {
    console.log(res.data);
    wx.navigateBack({
    delta: 1 //小程序关闭当前页面返回上一页面
    })
    wx.showToast({
    title: '评教成功!',
    icon: 'success',
    duration: 2000
    })
    },
    })
  • 相关阅读:
    将程序集从GAC中导出
    客户端访问WebService复杂参数类型的使用和参数的序列化与反序列化
    (译Workflow in the 2007 Microsoft Office System)介绍(部分)
    处理PageRequestManager事件
    测试 IPersonalizable接口
    几篇很好的文章
    交换两个数,不引入第三个变量
    Dijkstra算法
    给定一个整数,求它的二进制表示中有多少个1。
    自律
  • 原文地址:https://www.cnblogs.com/zqblog1314/p/14728072.html
Copyright © 2011-2022 走看看