Question

I created an simple Backbone script wich relies on a simple api created with laravel RESTful capabilities. The html containing Backbone script resides on my desktop and the server api on my xampp localhost.

on Laravel :

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
    $task = Task::find($id);

            return $task;
}

on Backbone :

(function(){

 App = {

  Models : {},

  Views : {},

  Collections : {}

 }; 

 App.Models.Task = Backbone.Model.extend({

  defaults : {title : '', active : 0},

  urlRoot : 'http://localhost/api/public/tasks'

 });

  var task = new App.Models.Task({id : 1});

  task.fetch();

  //task.set({title : 'new tite', active : 1});

  //task.save();

})();

The server side seams to be ok, cause on testing returns the proper json object, but when i try to run the Backbone script i get this error in dev console :

> XMLHttpRequest cannot load http://localhost/api/public/tasks/1. No
> 'Access-Control-Allow-Origin' header is present on the requested
> resource. Origin 'null' is therefore not allowed access

. What could be the problem ???

No correct solution

OTHER TIPS

Maybe someone will need this !

Thank's to TonyArra I found the answer:

  • since the server and the request script was on 2 different domains here is the solution:

  • add this on server (using laravel i added this code in the constructor of the restful controller):

            $http_origin = $_SERVER['HTTP_ORIGIN'];
    
           //if you trust origin set header else verify source
    
           //if ($http_origin == "http://www.domain1.com")
    
           header("Access-Control-Allow-Origin: $http_origin");
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top