尝试处理CORS请求时出现ParseError

时间:2016-02-08 12:48:01

标签: javascript ruby-on-rails cors cross-domain

我正在尝试从我的静态页面到我的rails api进行API调用。它们托管在不同的域上,因此我需要启用CORS - 它可以是预先发出的请求或简单的CORS请求。

我得到的错误是ActionDispatch::ParamsParser::ParseError (399: unexpected token at 'object Object]')。我不知道这是怎么回事。

我的rails API代码:

controller.rb:

class VisitorsController < ApplicationController

  skip_before_filter :verify_authenticity_token

  before_filter :set_headers
  after_filter :cors_set_access_control_headers

  def create
    puts 'VisitorsController#create'
    @visitor = Visitor.new(visitor_params)

    if @visitor.save
      render json: @visitor, status: :created
    else
      render json: @visitor.errors, status: :unprocessable_entity
    end
  end

private
  def visitor_params
    params.permit(:email, :phone)
  end

  def cors_set_access_control_headers
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'POST, OPTIONS'
    headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token'
    headers['Access-Control-Max-Age'] = '1728000'
  end

  def set_headers
    puts 'set_headers'
    if request.method == 'OPTIONS'
      headers['Access-Control-Allow-Origin'] = '*'
      headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
      headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version, Token, Content-Type'
      headers['Access-Control-Max-Age'] = '1728000'

      render :text => '', :content_type => 'text/plain'
    end
  end
end

routes.rb:

match 'visitors', to: 'visitors#create', via: [:options, :post]

上述设置(或类似设置)适用于较旧的项目,并且与此gist一致。我觉得错误在客户端代码中,所以我尝试了不同的方法:

script1.js:

            var url = "http://localhost:3000/v1/visitors/";
            var method = "POST";
            var postData = {email: email, phone: phno};

            var async = true;

            var request = new XMLHttpRequest();
            request.onload = function () {
                // You can get all kinds of information about the HTTP response.
                var status = request.status; // HTTP response status, e.g., 200 for "200 OK"
                var data = request.responseText; // Returned data, e.g., an HTML document.
                console.log("response " + data);
            };

            request.open(method, url, async);

            request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
            request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
            request.send(postData);

script2.js:

            $.ajax({
                url: 'http://localhost:3000/v1/visitors/',
                type: 'POST',
                data: {email: email, phone: phno},
                crossDomain: true,
                dataType: "json",
                contentType: 'application/json; charset=utf-8',
                headers: {"X-Requested-With": "XMLHttpRequest"},
                error: function (xhr) {
                    console.log('Error: ' + xhr.statusText);
                },
                success: function (result) {
                    // do something
                },
                async: true,
                processData: false
            });

但在这两种情况下我都会遇到同样的错误:

  

XMLHttpRequest无法加载http://localhost:3000/v1/visitors/。没有   请求中存在“Access-Control-Allow-Origin”标头   资源。因此不允许来源“http://localhost:63342”   访问。响应的HTTP状态代码为400。

服务器日志出错:

Started POST "/v1/visitors/" for 127.0.0.1 at 2016-02-08 17:49:43 +0530
  ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Error occurred while parsing request parameters.
Contents:

[object Object]

ActionDispatch::ParamsParser::ParseError (399: unexpected token at 'object Object]'):
  actionpack (4.2.5) lib/action_dispatch/middleware/params_parser.rb:53:in `rescue in parse_formatted_parameters'
  actionpack (4.2.5) lib/action_dispatch/middleware/params_parser.rb:32:in `parse_formatted_parameters'
  actionpack (4.2.5) lib/action_dispatch/middleware/params_parser.rb:23:in `call'
  activerecord (4.2.5) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
  activerecord (4.2.5) lib/active_record/migration.rb:377:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.5) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
  activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
  activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.5) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call'
  railties (4.2.5) lib/rails/engine.rb:518:in `call'
  railties (4.2.5) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  puma (2.16.0) lib/puma/server.rb:557:in `handle_request'
  puma (2.16.0) lib/puma/server.rb:404:in `process_client'
  puma (2.16.0) lib/puma/server.rb:270:in `block in run'
  puma (2.16.0) lib/puma/thread_pool.rb:106:in `call'
  puma (2.16.0) lib/puma/thread_pool.rb:106:in `block in spawn_thread'


  Rendered /home/vedant/.gem/ruby/2.2.4/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.5ms)
  Rendered /home/vedant/.gem/ruby/2.2.4/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.4ms)
  Rendered /home/vedant/.gem/ruby/2.2.4/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.6ms)
  Rendered /home/vedant/.gem/ruby/2.2.4/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (10.6ms)

1 个答案:

答案 0 :(得分:0)

好像这是一个真正的noob错误。我发送的json数据不正确。我所要做的就是改变

postData = {email: email, phone: phno};

postData = JSON.stringify({email: email, phone: phno});

使用客户端script1.js一切正常。

希望这个QnA可以作为任何尝试CORS调用rails api的人的参考。

相关问题