ruby-on-rails – 在尝试使用AngularJS将数据发布到RAils服务器
发布时间:2020-12-17 01:50:59 所属栏目:百科 来源:网络整理
导读:尝试从我的AngularJS端向Rails服务器发送数据时遇到问题. 服务器错误: ActionController::RoutingError (No route matches [OPTIONS] "/users"): actionpack (4.1.9) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' actionpack (4.1.9)
尝试从我的AngularJS端向Rails服务器发送数据时遇到问题.
服务器错误: ActionController::RoutingError (No route matches [OPTIONS] "/users"): actionpack (4.1.9) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' actionpack (4.1.9) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' railties (4.1.9) lib/rails/rack/logger.rb:38:in `call_app' railties (4.1.9) lib/rails/rack/logger.rb:20:in `block in call' activesupport (4.1.9) lib/active_support/tagged_logging.rb:68:in `block in tagged' activesupport (4.1.9) lib/active_support/tagged_logging.rb:26:in `tagged' activesupport (4.1.9) lib/active_support/tagged_logging.rb:68:in `tagged' railties (4.1.9) lib/rails/rack/logger.rb:20:in `call' actionpack (4.1.9) lib/action_dispatch/middleware/request_id.rb:21:in `call' rack (1.5.5) lib/rack/methodoverride.rb:21:in `call' rack (1.5.5) lib/rack/runtime.rb:17:in `call' activesupport (4.1.9) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call' rack (1.5.5) lib/rack/lock.rb:17:in `call' actionpack (4.1.9) lib/action_dispatch/middleware/static.rb:84:in `call' rack (1.5.5) lib/rack/sendfile.rb:112:in `call' railties (4.1.9) lib/rails/engine.rb:514:in `call' railties (4.1.9) lib/rails/application.rb:144:in `call' rack (1.5.5) lib/rack/content_length.rb:14:in `call' thin (1.6.3) lib/thin/connection.rb:86:in `block in pre_process' thin (1.6.3) lib/thin/connection.rb:84:in `catch' thin (1.6.3) lib/thin/connection.rb:84:in `pre_process' thin (1.6.3) lib/thin/connection.rb:53:in `process' thin (1.6.3) lib/thin/connection.rb:39:in `receive_data' eventmachine (1.0.8) lib/eventmachine.rb:193:in `run_machine' eventmachine (1.0.8) lib/eventmachine.rb:193:in `run' thin (1.6.3) lib/thin/backends/base.rb:73:in `start' thin (1.6.3) lib/thin/server.rb:162:in `start' rack (1.5.5) lib/rack/handler/thin.rb:16:in `run' rack (1.5.5) lib/rack/server.rb:264:in `start' railties (4.1.9) lib/rails/commands/server.rb:69:in `start' railties (4.1.9) lib/rails/commands/commands_tasks.rb:81:in `block in server' railties (4.1.9) lib/rails/commands/commands_tasks.rb:76:in `tap' railties (4.1.9) lib/rails/commands/commands_tasks.rb:76:in `server' railties (4.1.9) lib/rails/commands/commands_tasks.rb:40:in `run_command!' railties (4.1.9) lib/rails/commands.rb:17:in `<top (required)>' bin/rails:8:in `require' bin/rails:8:in `<top (required)>' spring (1.3.6) lib/spring/client/rails.rb:28:in `load' spring (1.3.6) lib/spring/client/rails.rb:28:in `call' spring (1.3.6) lib/spring/client/command.rb:7:in `call' spring (1.3.6) lib/spring/client.rb:26:in `run' spring (1.3.6) bin/spring:48:in `<top (required)>' spring (1.3.6) lib/spring/binstub.rb:11:in `load' spring (1.3.6) lib/spring/binstub.rb:11:in `<top (required)>' bin/spring:13:in `require' bin/spring:13:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' 所以这将是我的Rails服务器控制器和路由文件. 用户控制器 class UsersController < ApplicationController def index respond_to do |format| format.json {render json: { name: 'Jonhy'},callback: params[:callback]} end end def new end def create_user puts user_params @user = User.new(user_params) if @user.save render json: { error: false },layout: false else render json: { error: true },layout: false end end def show end def user_params params.require(:user).permit(:email,:password,:password_confirmation) end end 应用控制器 class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs,you may want to use :null_session instead. protect_from_forgery with: :exception before_filter :allow_ajax_request_from_other_domains def allow_ajax_request_from_other_domains headers['Access-Control-Allow-Origin'] = 'http://localhost:8001' headers['Access-Control-Request-Method'] = '*' headers['Access-Control-Allow-Headers'] = '*' end end 击溃 Rails.application.routes.draw do root 'home#index' resources :users do collection { post :create_user,via: :options } end end 这是我的AngularJS,我在那里制作POST Ajax. var App = angular.module('OutOfBox',['ng.deviceDetector','ngRoute','ngResource']); App.factory('Users',function($resource){ var users = $resource('http://127.0.0.1:8001/:user',{user:'users'},{ query: {method:'GET',isArray: true},save: {method:'POST',isArray: false} }); return users; } ); App.controller('MainCtrl',['$scope','$http','Users',function($scope,$http,Users) { $scope.user = []; $scope.responsive = []; $scope.submit = function() { if ($scope.users.email && $scope.users.password && $scope.users.password_confirmation) { $scope.user.push(this.users); Users.save({user: $scope.user},function(){ console.log(':)') },function(error){ console.log(error) }); $scope.users = ''; } }; }]); App.config(['$routeProvider','$locationProvider','$httpProvider',function($routeProvider,$locationProvider,$httpProvider) { $routeProvider. when('/home',{ templateUrl: 'views/main.html',controller: 'MainCtrl' }). otherwise({ redirectTo: '/home' }); // use the HTML5 History API $locationProvider.html5Mode(true); $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common["X-Requested-With"]; }]); 所以我尝试了很多解决方案,但没有人修复我的. 解决方法
Rails无法处理[OPTIONS]请求
您必须安装rack-cors才能处理CORS. 这是你的问题来自哪里: resources :users do collection { post :create_user,via: :options } # via: :options ? end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |