Jean
Jean

Reputation: 5411

json error on ruby on rails

I have the following action on my controller:

def bank_response
        @booking = Booking.where(order_number: params[:order]).first
        if([email protected]_Response.nil?)
            respond_to do |format|
            format.json {  render json: @booking, success: 200  }
          end
        end
    end

on my Newview I have the following code:

setInterval(function(){
      $.ajax({
        url: "/bank_response/#{@order_number}",
        type: "POST",
        contentType: 'application/json; charset=UTF-8',
        data: {"order" : "#{@order_number}"},
        dataType: "json",
        success: function(result){
          alert("success");
        },
        error: function(result) {
          alert("error");
        }
      });
    },10000);

also I have a the bank_response view like this:

{  "booking" : "#{@booking.to_s}" }

But I always get error 500 from my New view.

Remote Address:127.0.0.1:3000
Request URL:http://localhost:3000/bank_response/609nrhqlewbi
Request Method:POST
Status Code:500 Internal Server Error
Request Headersview source

Thanks for your help

UPDATE - RAILS SERVER


MultiJson::LoadError at /bank_response/ffgs1d79aatc===================================================> 795: unexpected token at 'order=ffgs1d79aatc'multi_json (1.8.2) lib/multi_json/load_error.rb, line 6-------------------------------------------------------ruby 1 module MultiJson 2 class LoadError < StandardError 3
attr_reader :data 4 def initialize(message='', backtrace=[], data='') 5 super(message)> 6
self.set_backtrace(backtrace) 7 @data = data 8 end 9 end 10 DecodeError = LoadError # Legacy support 11
end
App backtrace-------------Full backtrace-------------- - multi_json (1.8.2) lib/multi_json/load_error.rb:6:in initialize' - multi_json (1.8.2) lib/multi_json.rb:122:inrescue in load' - multi_json (1.8.2) lib/multi_json.rb:119:in load' - activesupport (3.2.13) lib/active_support/json/decoding.rb:15:indecode' - actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:47:in parse_formatted_parameters' - actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:17:incall' - actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in call' - rack (1.4.5) lib/rack/session/abstract/id.rb:210:incontext' - rack (1.4.5) lib/rack/session/abstract/id.rb:205:in call' - actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in call' - actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in block in call' - activesupport (3.2.13) lib/active_support/callbacks.rb:405:in _run__4308745349437725987__call__4526229337647608189__callbacks' - activesupport (3.2.13) lib/active_support/callbacks.rb:405:in __run_callback' - activesupport (3.2.13) lib/active_support/callbacks.rb:385:in_run_call_callbacks' - activesupport (3.2.13) lib/active_support/callbacks.rb:81:in run_callbacks' - actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:incall' - actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in call' - actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in call' - better_errors (1.0.1) lib/better_errors/middleware.rb:84:in protected_app_call' - better_errors (1.0.1) lib/better_errors/middleware.rb:79:inbetter_errors_call' - better_errors (1.0.1) lib/better_errors/middleware.rb:56:in call' - actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:incall' - actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in call' - railties (3.2.13) lib/rails/rack/logger.rb:32:incall_app' - railties (3.2.13) lib/rails/rack/logger.rb:16:in block in call' - activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in tagged' - railties (3.2.13) lib/rails/rack/logger.rb:16:in call' - quiet_assets (1.0.2) lib/quiet_assets.rb:18:in call_with_quiet_assets' - actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in call' - rack (1.4.5) lib/rack/methodoverride.rb:21:incall' - rack (1.4.5) lib/rack/runtime.rb:17:in call' - activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:incall' - rack (1.4.5) lib/rack/lock.rb:15:in call' - actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:incall' - railties (3.2.13) lib/rails/engine.rb:479:in call' - railties (3.2.13) lib/rails/application.rb:223:incall' - rack (1.4.5) lib/rack/content_length.rb:14:in call' - railties (3.2.13) lib/rails/rack/log_tailer.rb:17:incall' - thin (1.5.1) lib/thin/connection.rb:81:in block in pre_process' - thin (1.5.1) lib/thin/connection.rb:79:inpre_process' - thin (1.5.1) lib/thin/connection.rb:54:in process' - thin (1.5.1) lib/thin/connection.rb:39:inreceive_data' - eventmachine (1.0.3) lib/eventmachine.rb:187:in run' - thin (1.5.1) lib/thin/backends/base.rb:63:instart' - thin (1.5.1) lib/thin/server.rb:159:in start' - rack (1.4.5) lib/rack/handler/thin.rb:13:inrun' - rack (1.4.5) lib/rack/server.rb:268:in start' - railties (3.2.13) lib/rails/commands/server.rb:70:instart' - railties (3.2.13) lib/rails/commands.rb:55:in block in <top (required)>' - railties (3.2.13) lib/rails/commands.rb:50:in' - script/rails:6:in <main>' - script/rails:0:in'

UPDATE SOLVED


Guys, my problem was the type: "POST", because I'm making a "GET"

Thanks anyway.

Upvotes: 0

Views: 368

Answers (1)

joseramonc
joseramonc

Reputation: 1931

to respond with you'll have to do somethiing liek this:

format.json do
    render json: {
      booking_attr: @booking.attr,
      booking_second_attr: @booking_second_attr,
      id: @booking.id
    }
  end
end

Upvotes: 0

Related Questions