error "Not Found" while Javascript to Rails AJAX connection -
i have simple rails controller method. method works in browser , displays "ok" string. when call javascript ajax method, error "not found" displayed log javascript error method.why ?
what see in browser js log is:
"jqxhr: [object object]" application.js:3069 "textstatus: error" application.js:3070 "errorthrown: not found"
thanks responses:)
coupons_controler:
def get_value() #coupon_name = params[:param1] #@coupon = coupon.get_price(coupon_name) #respond_to |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 reconnect in ' + seconds + ' seconds.', false ); seconds--; if( seconds <= 0 ) { clearinterval( reconnectinterval ); hidemessage( 'fast' ); validatediscountcouponidajax( sec1, sec2 ); } }, 1000 ); } }); }
added: found error in js class:
no route matches [post] "/coupons"
why such error?
added 2: solved! ok solved:), using in route.rb, ajax trying post:). after changing both , works:)
found solution:
js ajax method using post method
... $.ajax({ type: 'post', url: 'http://localhost:3000/coupons/', ...
but in route.rb: url define method
get 'coupons' => 'coupons#get_value', :as => :coupons
so solution change in both places or post value.
Comments
Post a Comment