I'm having an issue with a simple Rails controller method. When I call it from JavaScript as an AJAX method, I get the error message "Not Found" in the JavaScript error log, even though the method works fine when accessed directly in the browser. What could be causing this?
Here's the output I see in the browser console:
"jqXHR: [object Object]" application.js:3069
"textStatus: error" application.js:3070
"errorThrown: Not Found"
Thank you for any insights!
Coupons_Controller:
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:)