ajax请求格式get,post,参数设置,转换参数类型

本文详细介绍了前端使用Ajax进行GET和POST请求的标准写法,包括简写法及关键参数的使用,如contentType和dataType,同时提供了数据类型转换的方法,如toString和JSON.stringify,以及前后端数据交互的格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.get请求(标准写法)
$.ajax({
    url:'http://127.0.0.1:5000/test',
    type:'get',
    dataType:'json',
    data: {"arr": arr.toString()},
    success:function(resp){
        alert(resp.data)
        console.log(resp.data)
    },
    error:function(resp){
        alert(resp)
    }
})
2.post请求(标准写法)
$.ajax({
    url:'http://127.0.0.1:5000/test',
    type:'post',
    dataType:'json',
    data: {"arr": arr.toString()},
    success:function(resp){
        alert(resp.data)
        console.log(resp.data)
    },
    error:function(resp){
        alert(resp)
    }
})
3.请求(简写法)
$.get('http://127.0.0.1:5000/test',param,function(resp){
     alert(resp)
 })
4.其中一些参数
contentType:****** -->发送给服务器的数据类型
dataType:***** -->服务器返回的数据类型
traditional:true -->当设置成true时,可以发送array数据类型,默认为false,会在前面加上[]
5.转换参数类型
arr.toString() -->转换成字符串类型
JSON.stringify(params) -->转换成json数据类型
>>前端传数据格式1:数据类型json

在这里插入图片描述

>>后端取数据格式1request.json

在这里插入图片描述

>>前端传数据格式2数据类型字典

在这里插入图片描述

>>后端取数据格式2request.args

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值