微信小程序中的AJAX——POST,GET区别

注意:发送服务器时的DATA

最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String 。转换规则如下:

  • 对于 GET 方法的数据,会将数据转换成 query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...
  • 对于 POST 方法且 header['content-type'] 为 application/json 的数据,会对数据进行 JSON 序列化
  • 对于 POST 方法且 header['content-type'] 为 application/x-www-form-urlencoded 的数据,会将数据转换成 query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...

在使用POST时 需要"Content-Type":  "application/x-www-form-urlencoded",默认的application/json在GET中有用,并且如果在POST请求中设置Header:application/json,服务端接受请求的时候总是以 ‘application/x-www-form-urlencoded’ 来处理。最重要的是:在发送DATA时,要用JSON字符串格式使用JSON对象形式也会出错。例子:data: { value : { "a":1,"b":2,"c":3 } }将会出错,要用JSON.stringify将其字符串化。

原文地址:https://www.cnblogs.com/cc-xiao5/p/10701766.html