【发布时间】:2016-08-04 07:56:54
【问题描述】:
我在我的示例应用程序中使用了backbonejs。以前,我在 nodejs 上托管它,但现在想在 tomcat 上运行它,但这个更改在客户端浏览器上给了我一个错误。我搜索并发现要解决这个问题跨域(https://stackoverflow.com/a/33820700/5086633)必须设置为true。不知道我应该如何以及应该如何处理这个问题。非常感谢这方面的任何帮助。
XMLHttpRequest cannot load http://localhost:8080/backbonejs/testpost.
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'null' is therefore not allowed access. The response
had HTTP status code 403.
使用 ajaxPrefilter、模型和集合如下。
$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
options.url = 'http://localhost:8080/backbonejs' + options.url;
});
var Banks = Backbone.Collection.extend({
url: '/testpost'
});
var Bank = Backbone.Model.extend({
urlRoot: '/testpost'
});
【问题讨论】:
-
在服务器端,你必须允许跨域。看看那个:stackoverflow.com/questions/7067966/…
-
是的,我以为你的后端是 node.js。我对tomcat没有经验,但这应该对你有帮助:enable-cors.org/server_tomcat.html
标签: tomcat backbone.js spring-security