0

基本的には、新規コース作成フォームを送信し、JQuery を使用して応答を処理したいと考えています。私のフォームのような

<div id="create_coursem">
  <%= form_tag({:controller => "coursem", :action => "create"}, :id => "check_coursem_form", :method => "post", :remote => true) do %>
      <p>
      <b> <%= label_tag(:name, "Name:") %> </b>
      <%= text_field_tag(:name) %>
      </p>
      <p>
      <b> <%= label_tag(:department, "Department:") %> </b>
      <%= select_tag "department", @result.html_safe %>
      </p>
      <%= submit_tag("Create") %>

  <% end %>

</div>

そして、私のコントローラーには、

  def create
    @coursem = Coursem.createCoursemByUser(params[:name], params[:department])
    @errCode = @coursem
    if @coursem.class != Fixnum
      @errCode = 1
    end
    @dic = {:errCode => @errCode, :coursem => @coursem}
    respond_to do |format|
      format.json { render json: @dic}
    end
  end

レスポンス ステータスは 200 で、json を正常に返したことを意味します。そして私のJSファイルには、

$(document).ready(function() {
    panelHandler();
    //showOverview();
    showResources();
    resourceHandler();
    calendarChangeMonth();
    subscribeHandler();

    shownewresourceform();

    eventinfo();
    closeeventbox();
    showneweventform();
    colorrecentevents();
    check_coursem();
})
    function check_coursem() {
        $("form#check_coursem_form").on('ajax:beforeSend', function(xhr, settings) {alert("hello");})
                                .on('ajax:success',    function(data, status, xhr) {alert("hello");})
                                .on('ajax:complete', function(xhr, status) {alert("hello");})
                                .on('ajax:error', function(xhr, status, error) {alert("hello");});
    }

しかし、作成ボタンをクリックすると、作成コントローラーによって生成されたjsonのみが返され、アラートは表示されません。その理由を知っている人はいますか?本当に感謝しています。

4

2 に答える 2

-1

私は ajax でたくさんの作業をしたことはありませんが、そのようにフォーマットされたリクエストを見たことはありません...ドキュメントのようなものを実装しようとしましたか?

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.ajax( "example.php" )
    .done(function() { alert("success"); })
    .fail(function() { alert("error"); })
    .always(function() { alert("complete"); });

// perform other work here ...

// Set another completion function for the request above
jqxhr.always(function() { alert("second complete"); });
于 2013-04-24T03:55:25.563 に答える