django tastypie和跨域json

在localhost:8000上运行了django dev服务器,在localhost:3000上运行了nodejs服务器。 我想要将json导入到nodejs服务器,但我得到这个错误:

XMLHttpRequest无法加载http://127.0.0.1:8000/api/presentation/?format=json 。 Access-Control-Allow-Origin不允许源http:// localhost:3000

这是我第一次进入跨域的乐趣,所以我想方设法摆脱我的深度。

我已经添加到我的节点(expressjs)的路线。

app.all('/', function(req, res){ res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); res.render('index', { title: '...' }); }); 

我错过了什么/错在哪里?

数据提供者需要为跨域请求( 而不是客户端)设置一个策略,如您的expressjs片段所示。

有人发表了一个简单的Django中间件注意到注入所需的标题:

Middlware允许你的Django服务器响应正确的跨域XHR(postMessage html5 API)。

https://gist.github.com/426829 – 这个片段是非常有用的,但是使用骨干到django服务器,我不得不匹配Access-Control-Request-Headers头与访问控制 – 允许头上的响应。

咖啡:

 auth = (xhr) -> xhr['xhrFields']= {withCredentials: true} xhr.setRequestHeader('Access-Control-Allow-Credentials', 'true' ) xhr.header('Access-Control-Allow-Origin', "*") xhr.header('Access-Control-Allow-Methods', 'GET,POST,PUT,HEAD,DELETE,OPTIONS') xhr.header('Access-Control-Allow-Headers', 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control') 

python: https : //gist.github.com/426829多一行

 def process_request(self, request): if 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' in request.META: response = http.HttpResponse() response['Access-Control-Allow-Origin'] = XS_SHARING_ALLOWED_ORIGINS response['Access-Control-Allow-Methods'] = ",".join( XS_SHARING_ALLOWED_METHODS ) response['Access-Control-Allow-Headers'] = "Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control" return response return None 

希望这是有用的!

你也可以使用JSONP

 http://127.0.0.1:8000/api/presentation/?format=jsonp 

这个要点是一个TastyPie资源基类。 任何资源子类化将是跨域访问。

它与其他人相似,但会将CORS头添加到TastyPie资源可能提供的所有响应中。 这包括错误响应和ImmediateHttpResponseexception

 from tastypie.resources import Resource from tastypie.exceptions import ImmediateHttpResponse from django.http import HttpResponse class CorsResource(Resource): """ adds CORS headers for cross-domain requests """ def patch_response(self, response): allowed_headers = ['Content-Type', 'Authorization'] response['Access-Control-Allow-Origin'] = '*' response['Access-Control-Allow-Headers'] = ','.join(allowed_headers) return response def dispatch(self, *args, **kwargs): """ calls super and patches resonse headers or catches ImmediateHttpResponse, patches headers and re-raises """ try: response = super(CorsResource, self).dispatch(*args, **kwargs) return self.patch_response(response) except ImmediateHttpResponse, e: response = self.patch_response(e.response) # re-raise - we could return a response but then anthing wrapping # this and expecting an exception would be confused raise ImmediateHttpResponse(response) def method_check(self, request, allowed=None): """ Handle OPTIONS requests """ if request.method.upper() == 'OPTIONS': if allowed is None: allowed = [] allows = ','.join([s.upper() for s in allowed]) response = HttpResponse(allows) response['Allow'] = allows raise ImmediateHttpResponse(response=response) return super(CorsResource, self).method_check(request, allowed) 

有一个友好的,可configuration的Django模块: django-cors-headers

在ajax POST代码中设置dataType:'text'而不是'jsonp'。