Note that there are some explanatory texts on larger screens.

plurals
  1. POAngular send POST request with resource service to Rails API
    primarykey
    data
    text
    <p>I´m trying to send a POST request using a resource service in Angular to a Rails API. Both, angular client and API are not in same server (so there is Cross Domain).</p> <p>I´m not able to send the request, and I think probably I have more than one problem (I think the angular controller or service are wrong) and probably I have a problem with CSRF (cross domain).</p> <p>I have read a lot of posts, and have added some suggestions, so now, I have a mix of all, that is not working.</p> <p><strong>For CSRF purpose</strong>: </p> <ol> <li><p>In rails I have added :</p> <ul> <li><p>Application controller: </p> <pre><code>class ApplicationController &lt; ActionController::Base protect_from_forgery after_filter :set_csrf_cookie_for_ng def set_csrf_cookie_for_ng cookies['XSRF-TOKEN'] = form_authenticity_token if protect_against_forgery? end protected def verified_request? super || form_authenticity_token == request.headers['X_XSRF_TOKEN'] end end </code></pre></li> </ul></li> <li><p>In angular:</p> <ul> <li><p>app.js:</p> <p><code>angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'myApp.controllers', 'myApp.i18n']). config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {<br> $routeProvider.when('/boat-booking', {templateUrl: 'partials/boat-booking.html', controller: 'BoatBookingCtrl'}); $routeProvider.otherwise({redirectTo: '/home'}); delete $httpProvider.defaults.headers.common["X-Requested-With"];<br> $httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content'); }]); </code></p></li> </ul></li> </ol> <p>For sending the POST request purpose:</p> <p>I have a controller with a variable I want to send:</p> <pre><code>.controller('BusinessCtrl', function ($scope, $location, Business) { $scope.createBusiness = function() { //var business = {name: "business1"}; alert ("business:" + $scope.business); alert ("businessName:" + $scope.business.name); // This is showing the business name, so the value is in scope. $scope.business = Business.save($scope.business); }; }); </code></pre> <p>Service:</p> <pre><code> .factory('Business', function($resource){ var businesses = $resource('http://127.0.0.1\\:3000/:business', {}, { query: {method:'GET', params:{business:'businesses'}, isArray: true}, save: {method:'POST', params:{business:'businesses'}, isArray: false} }); return businesses; } ); </code></pre> <p>When I execute this, I get:</p> <pre><code>Started OPTIONS "/businesses" for 127.0.0.1 at 2013-11-04 10:41:20 +0100 ActionController::RoutingError (No route matches [OPTIONS] "/businesses"): </code></pre> <p>in rails log. I have tried other combinations with different error messages but none of them worked.</p> <p>Thanks, Rober.</p> <p><strong>UPDATE</strong></p> <p>if I remove the parameters from angular service call in controller, like:</p> <pre><code>$scope.business = Business.save(); </code></pre> <p>Then, the logs in Rails change. Now, I send a POST request instead of OPTIONS. But, I have some other errors...</p> <pre><code>Started POST "/businesses" for 127.0.0.1 at 2013-11-04 11:00:31 +0100 Processing by BusinessesController#create as HTML Can't verify CSRF token authenticity Businesses_controller.create!!!!! Completed 400 Bad Request in 1ms </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload