#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
from django.http import HttpResponse def HttpResponseJsonCORS(request,s):
response = HttpResponse(s,content_type="application/json")
try:
origin = request.META['HTTP_ORIGIN']
except:
origin = 'http://127.0.0.1' if origin.find("192.168") >0 :
pass
elif origin.find("127.0.0.1") >0 :
pass
# else:
# origin = front_url
print origin
response["Access-Control-Allow-Origin"] = "%s"%(origin)
response["Access-Control-Allow-Credentials"] = "true"
response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
response["Access-Control-Max-Age"] = "1000"
response["Access-Control-Allow-Headers"] = "*"
return response def HttpResponseCORS(request, s):
response = HttpResponse(s)
try:
origin = request.META['HTTP_ORIGIN']
except:
origin = 'http://127.0.0.1'
if origin.find("192.168") > 0:
pass
elif origin.find("127.0.0.1") > 0:
pass
# else:
# origin = front_url # sql = "insert into sql_log (`sql`) values ('%s')"%(origin)
# print sql
# db.executesql(sql) response["Access-Control-Allow-Origin"] = "%s" % (origin)
response["Access-Control-Allow-Credentials"] = "true"
response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
response["Access-Control-Max-Age"] = "1000"
response["Access-Control-Allow-Headers"] = "*"
return response def index(request):
# 原始数据
rows = (('apollo', 'male', '164.jpeg'), ('apollo', 'male', ''))
# 表头
names = 'username gender pic'.split()
# URL公共部分
fs_url = 'http://www.baidu.com/' # 新数据列表
L = []
for e in rows:
L1 = list(e)
pic = e[2]
if pic == '':
L1[2] = "%suser_pic/default.jpg" % (fs_url)
else:
L1[2] = "%suser_pic/small_%s" % (fs_url, pic)
L.append(L1)
print L
# 用zip组合列表套字典
"""
[{'username': 'apollo', 'gender': 'male', 'pic': 'http://www.baidu.com/user_pic/small_164.jpeg'},
{'username': 'apollo', 'gender': 'male', 'pic': 'http://www.baidu.com/user_pic/default.jpg'}]
"""
data = [dict(zip(names, d)) for d in L]
print data
# 对上面结果Json序列化
"""
[{"username": "apollo", "gender": "male", "pic": "http://www.baidu.com/user_pic/small_164.jpeg"},
{"username": "apollo", "gender": "male", "pic": "http://www.baidu.com/user_pic/default.jpg"}]
"""
info = json.dumps(data, ensure_ascii=False)
print info
# 组合返回结果,返给前端
"""
response = {
"errcode": 0,
"errmsg": "获取用户列表成功",
"readLog":[{"username": "apollo", "gender": "male", "pic": "http://www.baidu.com/user_pic/small_164.jpeg"},
{"username": "apollo", "gender": "male", "pic": "http://www.baidu.com/user_pic/default.jpg"}]
}
"""
s = """
{
"errcode": 0,
"errmsg": "获取用户列表成功",
"readLog":%s
}
""" % (info) print s
return HttpResponseJsonCORS(request,s)

解决跨域HttpResponseJsonCORS, HttpResponseCORS 返回字典数据的更多相关文章

  1. 【C#】WebService接受跨域请求及返回json数据

    问题概述 通过Web Service发布服务供客户端调用是一种非常简单.方便.快速的手段,并且服务发布后会有一个服务说明页面,直观明了,如图: 一般情况下,在web页面中的JavaScript中调用W ...

  2. 前端笔记之服务器&Ajax(下)数据请求&解决跨域&三级联动&session&堆栈

    一.请求后端的JSON数据 JSON是前后端通信的交互格式,JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. JSON是互联网各个后台与 ...

  3. PhoneGap开发跨平台移动APP - 解决跨域资源共享

    解决跨域资源共享 一.WebApi解决跨域资源共享. 开发中选择WebApi来作为服务端的数据接口,由于使用PhoneGap,就需要通过js来获取远程远程数据服务器的数据,由于同源策略的限制,这就涉及 ...

  4. Ajax 是什么?Ajax 的交互模型?同步和异步的区别?如何解决跨域问题?以及 HTTP状态码

    一.Ajax 是什么: 1. 通过异步模式,提升了用户体验 2. 优化了浏览器和服务器之间的传输,减少不必要的数据往返,减少了带宽占用 3. Ajax 在客户端运行,承担了一部分本来由服务器承担的工作 ...

  5. AJAX 跨域请求 - JSONP获取JSON数据

    Asynchronous JavaScript and XML (Ajax ) 是驱动新一代 Web 站点(流行术语为 Web 2.0 站点)的关键技术.Ajax 允许在不干扰 Web 应用程序的显示 ...

  6. ajax 、ajax的交互模型、如何解决跨域问题

    1.ajax是什么? — AJAX全称为“AsynchronousJavaScript and XML”(异步JavaScript和XML),是一种创建交互式网页应用的网页开发技术. — 不是一种新技 ...

  7. js中ajax如何解决跨域请求

    js中ajax如何解决跨域请求,在讲这个问题之前先解释几个名词 1.跨域请求 所有的浏览器都是同源策略,这个策略能保证页面脚本资源和cookie安全 ,浏览器隔离了来自不同源的请求,防上跨域不安全的操 ...

  8. HTML5解决跨域问题

    HTML5解决跨域问题 由于浏览器的同源策略,网络连接的跨域访问是不被允许的,XHR对象不能直接与非同源的网站处理数据交互.而同源指的是什么呢?同源的范畴包括:规则(协议),主机号(域名.ip等),端 ...

  9. 用jQuery与JSONP轻松解决跨域访问的问题【转】

    原文地址:http://www.jb51.net/article/46463.htm 好在,有jquery帮忙,跨域问题似乎没那么难缠了.这次也借此机会对跨域问题来给刨根问底,结合实际的开发项目,查阅 ...

随机推荐

  1. python-list.sort && lambda

    dictionary是一个有元组组成的list,变量名有点歧义,这里是想表达 由元组组成的list哈. 之所以用dictionary是因为模拟的将字典转换成list. 将list进行排序,而根据lam ...

  2. C# 一个长度为100的int数组,插入1-100的随机数,不能重复,如何写

    int[] intArr = new int[100]; ArrayList myList = new ArrayList(); Random rnd = new Random(); while (m ...

  3. 在drop user之前,建议获取该用户的依赖情况

    在删除这两个用户之前,建议获取这两个用户的依赖情况: SQL> col owner format a15 SQL> col name format a15 SQL> col REFE ...

  4. springmvc中配置servlet初始化类

    <bean  id="InitStart" lazy-init="false" init-method="InitSystem" cl ...

  5. 在Linux下搭建git服务器

    http://www.cnblogs.com/dee0912/p/5815267.html 步骤很详细,很受用

  6. Model Binding is not working. MVC3 视图模型绑定不成功

    问题出现在POST方法中,当我要将数据提交到后台的时候,绑定的变量值为null 原因是视图中的名称跟Controller中的视图的名称不一致造成的. 假如你视图中的Model采用的是Html.Labe ...

  7. 数据库设计(五)第一范式(1NF)?

    In our last tutorial we learned and understood how data redundancy or repetition can lead to several ...

  8. ORA-01036: 非法的变量名/编号

    今天写程序时,往Oracle中插入二进制数据,出现错误ORA-01036:非法的变量名/编号,代码如下: strSql = "INSERT INTO KA99 (KA991,KA992,KA ...

  9. [转]JS脚本抢腾讯云学生1元代金券

    转自:http://blog.csdn.net/lkxlaz/article/details/54909397 今天抢代金券,在网上看到的,虽然脚本很easy,但也mark一下吧. //make th ...

  10. 编写可维护的JavaScript----笔记(一)

    1.缩进层级 建议使用4个空格为一个缩进层级,避免使用制表符进行缩进,可以通过配置文本编辑器来改变 缩进层级表示的内容. 2.语句末尾 有赖于分析器的自动分号插入机制(ASI),JavaScript可 ...