【大前端】vue,jq,uniapp ajax默认的请求格式

vue,jq,uniapp ajax默认的请求格式

常见的内容类型(content-type)

  • application/x-www-form-urlencoded 表单中默认的encType,表单数据被编码为key/value格式发送到服务器
  • application/json json格式
  • multipart/form-data 需要在表单中进行文件上传时,就需要使用该格式

jq

默认请求格式

 	$.ajax({
            type: "POST",
            url: "login",
        	//#login为表单id
            data: $("#login").serialize(),
            dataType: "json",
            success: function (response) {
               if(response){
                    window.location.href="success.html"
               }else{
                   alert("fail")
               }
            },
            error: function (response) {

            }
        });

默认内容类型:

application/x-www-form-urlencoded; charset=UTF-8

multipart/form-data请求格式

 $.ajax({
            type: "POST",
            url: "login",
            processData: false,
            contentType: false,
            data:  new FormData($("#login")[0]),
            dataType: "json",
            success: function (response) {
               if(response){
                    window.location.href="success.html"
               }else{
                   alert("fail")
               }
            },
            error: function (response) {

            }
        });

VUE

默认请求格式:

 this.axios({
        method: "请求的方法",
        url: "路径",
        data: {
          数据
        },
      }).then((response) => {
          console.log(response)
        })
        .catch((response) => {
          console.log(response);
        });

默认的内容格式

application/json;charset=utf-8

设置请求格式:

 this.axios({
        method: "请求的方法",
        url: "路径",
        data: {
          数据
        },
         headers: {
         // '键名':'键值'
        'Content-Type': 'application/x-www-form-urlencoded'
    },
      }).then((response) => {
          console.log(response)
        })
        .catch((response) => {
          console.log(response);
        });

uniapp

uniapp post默认的内容格式我也不太清楚是啥,但是如果不加请求头的话会请求失败,解决方法如下:

			uni.request({
					url:”路径",
					method:"POST",
					header:{
						"content-type": "application/x-www-form-urlencoded"
					},
					data:{
						//data
					},
					success(res) {
						
					},
					fail(err){
						
					}
				})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GengMS_DEV

你的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值