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数据的交互的更多相关文章

  1. ashx 获取ajax Post到后台json数据

    前台页面代码: var json = [{ "Name": "Pavan Kumar Pabothu", "Age": 27, " ...

  2. SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法

    最近angularjs post到后台 400一头雾水 没有任何错误. 最后发现好文,感谢作者 SpringMVC中出现" 400 Bad Request "错误(用@Respon ...

  3. 结合Bootbox将后台JSON数据填充Form表单

    本文介绍了如何结合Bootbox将后台JSON数据填充到Form表单中,同时也介绍了一些需要使用的知识的学习途径,并附上了参考文档地址与学习网址,对此感兴趣的伙伴可以直接访问学习.为了方便介绍,使用了 ...

  4. 11月13日上午ajax返回数据类型为JSON数据的处理

    ajax返回数据类型为JSON数据的处理 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...

  5. 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回

    作者:ssslinppp      时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...

  6. 解决后台json数据返回的字段需要替换的问题

    有时候后台json数据返回的字段含有“id”,也有可能是有时候为了减少代码的冗余,两页面之间只是数据模型个别属性的区别,所以这时候最好是用到模型属性的替换,用新的属性替换返回的json数据的字段.这里 ...

  7. AJAX跨域请求json数据的实现方法

    这篇文章介绍了AJAX跨域请求json数据的实现方法,有需要的朋友可以参考一下 我们都知道,AJAX的一大限制是不允许跨域请求. 不过通过使用JSONP来实现.JSONP是一种通过脚本标记注入的方式, ...

  8. 类型:JQuery;问题:ajax调用ashx文件;结果:ashx文件怎么获取$.ajax()方法发送的json数据

    ashx文件怎么获取$.ajax()方法发送的json数据 作者:careful 和ajax相关     新浪微博QQ空间QQ微博百度搜藏腾讯朋友QQ收藏百度空间人人网开心网0 $.ajax({  t ...

  9. 使用ajax解析后台json数据时:Unexpected token o in JSON at position 1

    json数据解析异常 今天在做json数据的时候,出现了如下错误,说是解析异常. VM1584:1 Uncaught SyntaxError: Unexpected token o in JSON a ...

随机推荐

  1. Spring 如何保证后置处理器的执行顺序 - OrderComparator

    Spring 如何保证后置处理器的执行顺序 - OrderComparator Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.htm ...

  2. mysql 设置用户并授权

    一, 创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - 指 ...

  3. CentOS 7安装配置Samba服务器(挂载共享文件夹)

    CentOS 7安装配置Samba服务器 CentOS 7下Samba服务器安装配置过程笔记. 假设我们有这样一个场景 共享名 路径 权限 SHAREDOC /smb/docs 所有人员包括来宾均可以 ...

  4. Koko Eating Bananas LT875

    Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The g ...

  5. sql 用Group by分组后,取每组的前几条记录

    转自:http://blog.163.com/jeson_lwj/blog/static/135761083201052411115783/ --查询每门课程的前2名成绩 CREATE TABLE S ...

  6. Scrapy的安装和基本使用方法

    Scrapy的安装 1. Windows下安装流程: 方法一: 命令行执行pip install scrapy 安装scrapy 注意:如果有anaconda,也可以打开“Anaconda promp ...

  7. 玩具谜题(NOIP2016)

    题目链接:玩具谜题 提高组日常水题. 直接模拟,有需要注意的点会在代码后讲解: #include<bits/stdc++.h> using namespace std; int main( ...

  8. python之初级篇2

    一.数字类型 1)整数 int 类型 - bit_length() # 查询以二进制表示一个数字的值所需的位数 - int.from_bytes(bytes,byteorder) # 返回给定字节数组 ...

  9. 19 模块之shelve xml haslib configparser

    shelve 什么是shelve模块 也是一种序列化方式使用方法 1.opne 2.读写 3.close特点:使用方法比较简单 提供一个文件名字就可以开始读写 读写的方法和字典一致 你可以把它当成带有 ...

  10. 744. Find Smallest Letter Greater Than Target

    俩方法都是用二分查找,一个调库,一个自己写而已. 方法一,调库 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NUL ...