0

I've got a backbone site that I'm trying to get up and running. The API I want to feed into it lives on the same server, but on a different port (port 8080).

When I try and access the API, I get errors about 'Same Origin Policy' and such, but since they're on the same server, I'm not sure what I'm doing wrong.

How can I access the data from the API with my model?

define(function (require) {

    "use strict";

    var $           = require('jquery'),
        Backbone    = require('backbone'),
        api         = "http://localhost:8080/api/v1/",

        Candidate = Backbone.Model.extend({
            urlRoot: api + "profiles/",
            idAttribute: 'password',

            initialize: function () {
                console.log('Candidate initialized');
            },

            validate: function (attributes) {
                console.log('validate');
                console.log(attributes);
            },
        }),

        CandidateCollection = Backbone.Collection.extend({
            model: Candidate,
            url: api + "profiles/",
        });

    return {
        Candidate: Candidate,
        CandidateCollection: CandidateCollection
    };
});
Lisa
  • 2,102
  • 7
  • 26
  • 44
  • you need to lookup for CORS or/and setup a proxy server like nginx or apache depending on what server-side language you are using.Search for CORS first,you'll have to setup special response headers. – mpm May 29 '14 at 16:28
  • You need to use CORS. http://stackoverflow.com/questions/5714135/same-origin-host-different-ports-in-js EDIT: As above ^^ – Matt Derrick May 29 '14 at 16:31
  • Even though it's on the same server? I guess I don't understand why it's doing this. – Lisa May 29 '14 at 16:31
  • A different port is as good as being on a different domain. http://en.wikipedia.org/wiki/Same_origin_policy Here is a link on how to do this with NGINX http://serverfault.com/questions/384105/cross-origin-resource-sharing-cors-with-nginx-chrome – Matt Derrick May 29 '14 at 16:32

1 Answers1

1

Can you add apache and do a ProxyPass rule for all /api routed to http://localhost:8080/api

ProxyPass /api http://localhost:8080/api
ProxyPassReverse /api http://localhost:8080/api
jurassix
  • 1,509
  • 10
  • 10
  • NGINX should have a similar config. IMHO this is correct place to handle your requirement. Otherwise your going to have to look up CORS support and apply the correct headers to get past your cross domain issue – jurassix May 29 '14 at 16:47