前端AJAX请求数据,提示错误:“AttributeError: 'dict' object has no attribute 'status_code'”。

原因:是提示返回对象dict没有“status_code”属性,所以返回对象有问题。

views层的函数,有两个基本限制:
1.第一个数必须是request
2.必须返回HttpResponse类的一个实例(对象).
只返回了 字典类型的数据内容,没有用HttpResponse包裹一下字典。
return语句没有 render template或者  render json

改正后的代码如下,加了HttpResponse:

    if request.is_ajax():
cpu_used = getCPUstate();
res_json = '{"cpu_used":cpu_used}'
# , 'mem_used':mem_used
return HttpResponse(res_json)

还需要注意一个问题:

传到前端的json必须要加单引号'',因为前端解析json是以字符串解析的,否则传输到前端的就是json对象会有问题。

前端json解析代码如下:

 $.ajax({
url:"/linux_monitor/",
type:"GET",
dataType:"", //
processData:false,
contentType:false,
success:function (rdata) {
json_data = JSON.parse(rdata);
},
error:function(){
}
});

AttributeError: 'dict' object has no attribute 'status_code'的更多相关文章

  1. AttributeError: 'dict' object has no attribute 'has_key'

    运行下面的代码: if (locals().has_key('data')): del data gc.collect() 出错: if (locals().has_key('data')): Att ...

  2. AttributeError: 'dict' object has no attribute 'iteritems'

    在python3.6中运行 sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse= ...

  3. AttributeError: 'dict' object has no attribute 'encode'

    首先这是一个很简单的 运行时错误: 错误分析: AttributeError:属性错误,造成这种错误的原因可能有: 你尝试访问一个不存在的属性或方法.检查一下拼写!你可以使用内建函数 dir 来列出存 ...

  4. flask 表单填充数据报错!AttributeError: 'dict' object has no attribute 'getlist'

    报错信息: AttributeError: 'dict' object has no attribute 'getlist' 解决: 虽然是小毛病,不得不说还是自己太粗心大意了.

  5. facenet pyhton3.5 训练 train_softmax.py 时报错AttributeError: 'dict' object has no attribute 'iteritems'

    报错原因:在进行facenet进行train_softmax.py训练时,在一轮训练结束进行验证时,报错AttributeError: 'dict' object has no attribute ' ...

  6. 机器学习实战:KNN代码报错“AttributeError: 'dict' object has no attribute 'iteritems'”

    报错代码: sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True) 解决 ...

  7. 'dict' object has no attribute 'a'

    a = {} #a.a = 'a' #AttributeError: 'dict' object has no attribute 'a' #a['a'] #KeyError: 'a' a['a'] ...

  8. Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

  9. AttributeError: 'list' object has no attribute 'write_pdf'

    我在可视化决策树,运行以下代码时报错:AttributeError: 'list' object has no attribute 'write_pdf' 我使用的是python3.4 from sk ...

随机推荐

  1. Python之基于十六进制判断文件类型

    核心代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : suk import struct from io import Byt ...

  2. js 引号 转义字符 json字符串

    element_obj.NewTitle.value = json_obj.NewTitle.replace(/\"/g, "\""); model.NewTi ...

  3. border-box与content-box的区别

    ㈠box-sizing 属性 ⑴box-sizing 属性允许您以特定的方式定义匹配某个区域的特定元素. ⑵语法:box-sizing: content-box|border-box|inherit; ...

  4. StreamWriter、StreamReader

    IO流操作文件内容,using System.IO;//引入命名空间 private void button1_Click(object sender, EventArgs e) { if (text ...

  5. Python初记

    ------Python是一个优雅的大姐姐 我是通过<老男孩Python>学习Python,根据我手上的资源学习Python,资料不齐,但是这个是最好的,边学习边寻找有没有相同的类型. 在 ...

  6. 【GDSOI2019】滑稽二乘法【数据结构】【LCT】

    Description 点数<=100000,操作数<=200000 Solution 经典的LCT维护子树路径信息的问题. 具体来说,我们对于每一个节点,它在splay上的子树对应了原树 ...

  7. python动态的添加方法

    1.动态的创建实例方法 1 class Person(object): 2 def __init__(self,name,age): 3 self.name = name 4 self.age =ag ...

  8. 数据聚类算法-K-means算法

    深入浅出K-Means算法 摘要: 在数据挖掘中,K-Means算法是一种 cluster analysis 的算法,其主要是来计算数据聚集的算法,主要通过不断地取离种子点最近均值的算法. K-Mea ...

  9. git 更改远程仓库地址,强行推送远程仓库

    强行推送远程仓库 #把一个现有的工程拷贝一份 #去掉远程仓库关联 git remote rm origin #添加远程仓库关联 git remote add origin http://xxx.git ...

  10. Java——流、文件与正则表达式

    0. 字节流与二进制文件 我的代码 package javalearn; import java.io.DataInputStream; import java.io.DataOutputStream ...