So I have the a file, index.html
:
<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<head>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/css/bootstrap.min.css">
<link rel="stylesheet" href="/assets/css/main.css">
</head>
<body>
<div class="container" id="content">
<h1>User Manager</h1>
<hr/>
<div class="page"></div>
</div>
<!-- Third-Party Libraries -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"></script>
<!-- Application core -->
<script src="./src/common.js"></script>
<script src="./src/application.js"></script>
<!-- Modules -->
</body>
</html>
that references two javascript files, common.js
and application.js.
common.js
just holds all-purpose functions, and for now looks like:
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
application.js
holds a Backbone.js application and looks as follows, following the example at http://backbonetutorials.com/ :
/** PREFILTER - points to our instance **/
$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
options.url = 'http://backbonejs-beginner.herokuapp.com' + options.url;
});
/** COLLECTIONS **/
var Users = Backbone.Collection.extend({
url: '/users'
});
/** ROUTES **/
var Router = Backbone.Router.extend({
routes: {
'': 'home'
}
})
/** VIEWS **/
var userList = Backbone.View.extend({
el: '.page',
render: function() {
var users = new Users();
users.fetch({
success: function () {
this.$el.html("CONTENT HERE");
}
})
}
});
/** INSTANCES **/
/* VIEWS */
var userList = new userList();
/* ROUTES */
var router = new Router()
router.on('route:home', function() {
userList.render();
})
/** START HISTORY **/
Backbone.history.start();
I can use the Chrome developer tools to tell that this works with the example page provided by the site -- it pulls the json object and previews it.
However, when I switch the prefilter string over to my own heroku instance, I get the following in the console:
XMLHttpRequest cannot load {{our instance}}/users. Origin null is not allowed by Access-Control-Allow-Origin.
This happens even though I can go to {{our instance}}/users
in a browser and query it via Postman. My guess is that this has to do with the way that I'm having Backbone retrieve the data, that it is pulling it via a standard request rather than explicitly asking for JSON. Is there a best practice here so that I can grab the objects at this endpoint?
I'm pretty sure that XMLHttpRequest cannot load {{our instance}}/users. Origin null is not allowed by Access-Control-Allow-Origin.
XMLHttpRequest cannot load {{our instance}}/users. Origin null is not allowed by Access-Control-Allow-Origin.
is related to cdnjs not using the Access-Control-Allow-Origin: *
header.
Try to serve those files yourself (instead of cdnjs) and see if that solves your problem
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.