Vedant Agarwala
Vedant Agarwala

Reputation: 18819

ParseError while trying to handle a CORS request

I am trying to make an API call from my static page to my rails api. They are hosted on different domains so I need to enable CORS — it can be a pre-flighted request or a simple CORS request.

The error I am getting is ActionDispatch::ParamsParser::ParseError (399: unexpected token at 'object Object]'). I have no idea how this is happening.

My rails API code:

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]

The above setup (or a similar one) did work for an older project and it is also consistent with this gist. I felt the error is in the client code so I tried to different methods:

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
            });

But in both cases I get the same error:

XMLHttpRequest cannot load http://localhost:3000/v1/visitors/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 400.

Error in server logs:

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)

Upvotes: 1

Views: 429

Answers (1)

Vedant Agarwala
Vedant Agarwala

Reputation: 18819

Seems like it was a real noob error. I was sending incorrect json data. All I had to do was to change

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

to

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

Using the client side script1.js everything works fine.

Hope this QnA serves as a reference for anyone attempting a CORS call to a rails api.

Upvotes: 1

Related Questions