ajax post发送数据,使用POST和AJAX将数据发送到服务器

本文展示了如何在Django中处理POST类型的Ajax请求,将请求体中的数据转换为QueryDict对象,并通过过滤查询获取相关项目描述。在成功的情况下,返回包含'Success'的结果和项目列表;失败时返回'Unsuccessful'和错误信息。

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

我认为ajax数据放在请求体中。在

试试看你有没有拿到你的数据from django.http import QueryDict

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt

def validate_username(request):

if request.method == "POST" and request.is_ajax():

# The body of the request is in byte form,

# so we decode/convert to string using utf-8 encoding

datastring = request.body.decode("utf-8")

# The data comes in as if it was coming

# from a real form i.e. each field on the model

# comes separated by & e.g. the_post=demo&othefield=4 etc

# use djangos QueryDict to get a dictionary you can easily

# work with.

datadictionary = QueryDict(datastring)

# retrieve value like any other dictionary

description = datadictionary.get("the_post")

projects = Project.objects.filter(user__id__icontains = request.user.id)\

.filter(pro_description__icontains = description)\

.values_list('pro_description')

response_data = {}

try:

response_data['result'] = 'Success'

response_data['message'] = list(projects)

except:

response_data['result'] = 'Unsuccessful'

response_data['message'] = 'The Subprocess module did not run the script correctly.'

return HttpResponse(json.dumps(response_data), content_type="application/json")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值