0

I have some simple Rails controller method. This method works in browser and displays "OK" string. But when I call it from JavaScript as AJAX method, error "Not Found" is displayed to log from JavaScript error method.Why ?

What I see in browser JS log is:

"jqXHR: [object Object]" application.js:3069
"textStatus: error" application.js:3070
"errorThrown: Not Found"

Thanks for responses:)

Coupons_Controler:

def get_value()
    #coupon_name = params[:param1]
    #@coupon = Coupon.get_price(coupon_name)

  #respond_to do |format|
      #format.js { render :layout=>false }
      #render text: "17.8, true";
      render text: "OK";
    #end
  end

Javascript script method:

function validateDiscountCouponIDAjax( sec1, sec2 ) {
                    if( typeof sec1 === 'undefined' || typeof sec2 === 'undefined' ){
                sec1 = 2;
                sec2 = 3;
            }

                    $.ajax({
                            type: 'POST',
                            url: 'http://localhost:3000/coupons/',
                            //data: $('discount_coupon_id').serialize(),
                            success: function(resp) {
                                    if (resp === 'OK') {                                        
                                        console.log('Validation ok');
                                        showMessage( true, 'Discount coupon validated!' );  
                                    }
                                    else {
                                            console.log('Response error: ' + resp);
                                            //$('#password-dialog-error').text(resp);
                                    }
                            },
                            error: function( jqXHR, textStatus, errorThrown ) {
                    var seconds = sec1 + sec2;
                    sec1 = sec2;
                    sec2 = seconds;

                    console.log('jqXHR: '+jqXHR);
                    console.log('textStatus: '+textStatus);
                    console.log('errorThrown: '+errorThrown);

                    var reconnectInterval = setInterval( function() {
                        changeMessage( false, 'Connection error. Trying to reconnect in ' + seconds + ' seconds.', false );
                        seconds--;

                        if( seconds <= 0 ) {
                            clearInterval( reconnectInterval );
                            hideMessage( 'fast' );
                            validateDiscountCouponIDAjax( sec1, sec2 );
                        }
                    }, 1000 );
                            }
                    });
            }

ADDED: I found error in JS class:

No route matches [POST] "/coupons"

Why such error?

ADDED 2: SOLVED! OK solved:), I was using GET in Route.rb, but AJAX was trying to do POST:). After changing both to GET , it works:)

8
  • What do the server logs have to say about this? Commented Nov 27, 2014 at 9:40
  • Im not sure how to find logs, Im using Ubuntu and I was analyzing logs port 80, but this is temporary instance created for/by Rails. Where I can find logs in this situation? Commented Nov 27, 2014 at 9:46
  • Right where you ran rails server, the terminal session. Commented Nov 27, 2014 at 9:48
  • Added error message to problem description at the top:) Commented Nov 27, 2014 at 9:54
  • I changed POST to GET and it works:).. so the problem is with Route.rb:) BUt I want to use POST:) Commented Nov 27, 2014 at 9:58

1 Answer 1

2

Found solution:

JS AJAX method was using POST method

...
$.ajax({
type: 'POST',
url: 'http://localhost:3000/coupons/',
...

But in Route.rb: URL was define as GET method

get 'coupons' => 'coupons#get_value', :as => :coupons

So the solution is to change in both places to GET or POST value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.