Django 视图发送带有正确标头的空回复
我在 Dreamhost 服务器上有 Django 项目,它有几个返回 Json 响应的视图。昨天,我已将 Django 项目从本地计算机(localhost)移植到运行 apache 的 dreamhost 服务器。现在,如果我通过 jquery 调用我的 django 视图来获取
http: //www.abc.com/projects/
它应该返回我的 mongodb 数据库中的所有项目,但它返回的不是:
在 Firefox 上 - 只是没有响应的标头
Connection Keep-Alive
Content-Type application/json
Date Thu, 19 Jan 2012 09:03:34 GMT
Keep-Alive timeout=2, max=100
Server Apache
Status 200 OK
Transfer-Encoding chunked
在 Chrome 上 - 没有标头和响应数据。它会引发错误:
XMLHttpRequest cannot load http://abc.com/Projects/. Origin null is not allowed by Access-Control-Allow-Origin.**
如果我只是通过网络浏览器访问 http://www.abc.com/projects/它以 json 格式返回结果,但如果我使用 JavaScript/Jquery,则不会。
早些时候,我使用这个 中间件 来允许其他域通过 django 在我的本地计算机上请求并获取响应-build 服务器。但是现在当我在 apache 服务器上运行时它停止工作,所以我从 Settings.py 文件中删除了它。
我不知道为什么会出现这个错误。请帮忙
*编辑*
正如@burhan建议的那样,我在客户端使用了jsonp,现在我的服务器正在返回json,但浏览器是在解析之前给出错误。错误是:意外的令牌
JSON 我得到的答复是:
{"projects": [{"projectName": "carmella", "projectId": "4f13c7475fcff30710000000"}, {"projectName": "SeaMonkey", "projectId": "4f1677b75fcff37c03000001"}]}
I have Django project on Dreamhost server which has several views that returns Json response.Yesterday I have ported my Django project from local machine(localhost) to dreamhost server running apache.Now if I call my django view through jquery for
http://www.abc.com/projects/
It should return me all projects that i have in my mongodb database but instead of that it returns :
On Firefox - just headers with no response
Connection Keep-Alive
Content-Type application/json
Date Thu, 19 Jan 2012 09:03:34 GMT
Keep-Alive timeout=2, max=100
Server Apache
Status 200 OK
Transfer-Encoding chunked
On Chrome - No headers and response data.It throws an error:
XMLHttpRequest cannot load http://abc.com/Projects/. Origin null is not allowed by Access-Control-Allow-Origin.**
If I just access the http://www.abc.com/projects/ through my web-browser it returns me results in json format,but not in case if I use JavaScript/Jquery.
Earlier I was using this middleware to allow other domains to request and get response on my local-machine with django in-built server.But now when I am running on apache server It stops working so I removed It from Settings.py file.
I don't know why is this error coming .Please help
*EDIT*
As @burhan suggested I used jsonp on client side and now my server is returning json but browser is giving error before parsing it.Error is : unexpected token
JSON i am getting in reply is :
{"projects": [{"projectName": "carmella", "projectId": "4f13c7475fcff30710000000"}, {"projectName": "SeaMonkey", "projectId": "4f1677b75fcff37c03000001"}]}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在遇到同源策略沙箱。由于您的服务器是 www.abc.com 并且您正在访问 abc.com - 来源不同,这就是脚本未执行的原因。
您有几个选择:
You are running into the same origin policy sandbox. Since your server is www.abc.com and you are accessing abc.com - the origin is not the same, which is why the script is not executing.
You have a few options: