8.ajax与django后台json数据的交互
1新建django项目名为json_ajax,应用名为app,在templates模板中新建ajax.html文件
ajax.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ajax</title>
{% load staticfiles %}
<script type="text/javascript" src="{% static 'js/jquery-3.3.1.min.js' %}"></script>
</head>
<body>
<button id="send">发送</button>
<script>
$("#send").click(function () {
{#json数据#}
var post_data={
"name":"weihu",
}; $.ajax({
url:"http://127.0.0.1:8000/ajax",
type:"POST",
{#发送json数据到服务器#}
data:post_data, {#请求成功回调函数#}
success:function (data) {
alert(data)
alert("请求成功")
},
{#请求失败回调函数#}
error:function () {
alert("服务器请求超时,请重试!")
} });
});
</script>
</body>
</html>
2.在settings配置文件中,注释
# 'django.middleware.csrf.CsrfViewMiddleware',
STATIC_URL = '/static/' STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
3.在urls.py文件中,配置path路径
"""json_ajax URL Configuration The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
. Add an import: from my_app import views
. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
. Add an import: from other_app.views import Home
. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
. Import the include() function: from django.urls import include, path
. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from app import views urlpatterns = [
path('admin/', admin.site.urls),
path('test',views.test),
path('ajax',views.ajax),
]
4.在views.py中,实现逻辑代码
from django.shortcuts import render,HttpResponse
import json def test(request):
return render(request,'ajax.html') def ajax(request):
if request.method=="POST":
name=request.POST.get('name')
print("ok")
status=
result="sucuss"
return HttpResponse(json.dumps({
"status":status,
"result":result,
"name":name
}))
8.ajax与django后台json数据的交互的更多相关文章
- ashx 获取ajax Post到后台json数据
前台页面代码: var json = [{ "Name": "Pavan Kumar Pabothu", "Age": 27, " ...
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
最近angularjs post到后台 400一头雾水 没有任何错误. 最后发现好文,感谢作者 SpringMVC中出现" 400 Bad Request "错误(用@Respon ...
- 结合Bootbox将后台JSON数据填充Form表单
本文介绍了如何结合Bootbox将后台JSON数据填充到Form表单中,同时也介绍了一些需要使用的知识的学习途径,并附上了参考文档地址与学习网址,对此感兴趣的伙伴可以直接访问学习.为了方便介绍,使用了 ...
- 11月13日上午ajax返回数据类型为JSON数据的处理
ajax返回数据类型为JSON数据的处理 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...
- 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回
作者:ssslinppp 时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...
- 解决后台json数据返回的字段需要替换的问题
有时候后台json数据返回的字段含有“id”,也有可能是有时候为了减少代码的冗余,两页面之间只是数据模型个别属性的区别,所以这时候最好是用到模型属性的替换,用新的属性替换返回的json数据的字段.这里 ...
- AJAX跨域请求json数据的实现方法
这篇文章介绍了AJAX跨域请求json数据的实现方法,有需要的朋友可以参考一下 我们都知道,AJAX的一大限制是不允许跨域请求. 不过通过使用JSONP来实现.JSONP是一种通过脚本标记注入的方式, ...
- 类型:JQuery;问题:ajax调用ashx文件;结果:ashx文件怎么获取$.ajax()方法发送的json数据
ashx文件怎么获取$.ajax()方法发送的json数据 作者:careful 和ajax相关 新浪微博QQ空间QQ微博百度搜藏腾讯朋友QQ收藏百度空间人人网开心网0 $.ajax({ t ...
- 使用ajax解析后台json数据时:Unexpected token o in JSON at position 1
json数据解析异常 今天在做json数据的时候,出现了如下错误,说是解析异常. VM1584:1 Uncaught SyntaxError: Unexpected token o in JSON a ...
随机推荐
- mysql 设置用户并授权
一, 创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - 指 ...
- 补课:Shell命令${}
Shell中的${}.##和%%使用范例: 代码如下:file=/dir1/dir2/dir3/my.file.txt可以用${ }分别替换得到不同的值:${file#*/}:删掉第一个 / 及其左边 ...
- Continuous Subarray Sum LT523
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- rap2与postman自动化测试
rap2的接口数据可以全部导入postman: 在collections集合里面点击run;
- solr之创建core(搜索核心,包括索引和数据)的方法
我的solrhome为D:\solrHome\solr step1:进入solrHome会看到collection1文件夹,创建该文件夹的副本,重命名为product 进入product文件夹,进入d ...
- Linux CPU Hotplug CPU热插拔
http://blog.chinaunix.net/uid-15007890-id-106930.html CPU hotplug Support in Linux(tm) Kernel Linu ...
- OSS 视频存储
我这里加了 封面图片 可以不理睬! 我没有存oss. 阿里的OSS 自己可以去官网下载 我这里放到 Vendor 下的. 1 # 注意这里OSS中 请设置 存储空间名称为公共的 才能直接使用这里返回的 ...
- hashable/iterable与orderable
################ # hashable协议 # ################ # 一个对象能被称为hashable,它必须实现__hash__与_eq__方法: >>& ...
- boost--线程
1.thread的使用 boost的thread包含了线程创建.使用.同步等内容,使用thread需要包含头文件"boost\thread.hpp". thread中使用了需要编 ...
- 容器监控告警方案(cAdvisor + nodeExporter + alertmanager + prometheus +grafana)
一.prometheus基本架构 Prometheus 是一套开源的系统监控报警框架.它启发于 Google 的 borgmon 监控系统,由工作在 SoundCloud 的 google 前员工在 ...