Django solves the problem of cross-domain requests solution 1. Install Django-cors-headers
pip install django-cors-headers
内容详见:https://github.com/ottoyiu/django-cors-headers
Other solutions 1. Using JSONP
When using AJAX to get JSON data, using JSONP,JSONP can only be used for get requests, and the red part is a concern
$.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 '); } });
Background code:
defHello (Request): LST=you_get.main (repo_path=""+ _filepath +"", opt='- u', url="http://v.youku.com/v_show/id_XMjg4MTUwNjEzNg==.html") Data={"Data": LST}ImportJSON Ljson=json.dumps (data) result="Success_jsonpcallback ("+ Ljson +")"; Response=HttpResponse (Result)returnResponse
Python Django Cross Domain Solution