我正在尝试使用 coffeescript 和 jquery 发表一篇文章,但我的脚本没有处理成功回调。
*cards_controller.js.coffee*
if $('form').attr('action') == '/cards' alert "Hey ho" $('#add_card').click (e) ->
e.preventDefault()
$.ajax '/cards',
type: 'PUT' #Tried POST also
dataType: 'json'
error: (jqXHR, textStatus, errorThrown) ->
console.log textStatus
success: (res) ->
console.log "Card name: " + data.card.code + "\nPoints: " + data.points
complete: (jqXHR, textStatus) ->
console.log "Status: " + textStatus
index.html.erb
<%= form_tag url_for(:controller => 'cards', :action => 'create'), :remote => true %>
生成的javascript如下:
(function() {
if ($('form').attr('action') === '/cards') {
alert("Hey ho");
$('#add_card').click(function(e) {
e.preventDefault();
return $.ajax('/cards', {
type: 'POST',
dataType: 'json',
error: function(jqXHR, textStatus, errorThrown) {
return alert(textStatus);
},
success: function(res) {
console.log("Card name: " + data.card.code + "\nPoints: " + data.points);
return $('#card-list > tbody:last').append('<tr>test</tr><tr>uauauiua</tr>');
},
complete: function(jqXHR, textStatus) {
return console.log("Status: " + textStatus);
}
});
});
}
}).call(this);
*cards_controller.rb*
def create
card = Card.find_by_code(params[:code])
if card && card.user.nil?
current_user.add_card(card)
else
return render :status => 401, :json => { :message => "O cartão não existe ou já está cadastrado"}
end
respond_to do |format|
format.js { render json: { :card => card, :points => card.card_business_points.sum("points") } , content_type: 'text/json' }
end
end
请求成功发送到控制器,并且正确创建了卡。问题是 Chrome 上的调试似乎不处理回调并且不记录。
请问有人可以帮助e吗?