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:)
rails server, the terminal session.