码迷,mamicode.com
首页 > 编程语言 > 详细

python Django 跨域解决方案

时间:2017-07-18 10:10:26      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:otto   跨域请求   request   请求   https   ica   head   sync   解决   

django解决跨域请求的问题

解决方案

1.安装django-cors-headers

pip install django-cors-headers

内容详见:https://github.com/ottoyiu/django-cors-headers

 

其他解决方

1.使用JSONP

使用Ajax获取json数据时,使用JSONP,JSONP只能用于GET请求,红色部分是注意事项

$.ajax({
        url:‘http://localhost:8000/hello/‘,
        data:data,
        type : ‘GET‘,
        async : true,
        contentType: "application/json; charset=utf-8",
        dataType : ‘jsonp‘,
        jsonp:"callback",
        jsonpCallback:"success_jsonpCallback",
        success: function(data){    
            if (data.data) {    
                alert(true);            
            } else {
                return;
            }
        } ,
        error:function(e){
            alert(‘error‘);
        }
    });

后台代码:

def hello(request):
    lst=you_get.main(repo_path="" + _filepath + "", opt=-u, url="http://v.youku.com/v_show/id_XMjg4MTUwNjEzNg==.html")
    data={"data":lst}
    import json
    ljson=json.dumps(data)
    result = "success_jsonpCallback(" + ljson + ")";
    response = HttpResponse(result)
    return response

 

python Django 跨域解决方案

标签:otto   跨域请求   request   请求   https   ica   head   sync   解决   

原文地址:http://www.cnblogs.com/shurua/p/7198442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!