2017-01-31 21 views
0

ノード/エクスプレス/モンゴーアプリがあり、AJAX呼び出しをしようとしていますが、データは保存されますが、AJAXのpreventDefault()コール。なぜこうなった?ここでpreventDefault()を呼び出すときにAJAXフォームを再読み込みするページ

var categoryButton = function(){ 
event.preventDefault(); 
$("#add_category_form").submit(function (event){ 
    event.preventDefault(); 

    $.ajax({      //this gets the ID, you have to add a data-id="user._id" to the input you want to send the request from. 
     url: $("add_category_form").attr("action"), 
     type: "POST", 
     dataType: json, 
     done: function (result) { 
      alert("successful"); 
      //console.log(result.categories); 
     }, 
     fail: function (fail){ 
      console.log(fail); 
     } 
    }); 
}); 
}; 

はHTML

<section id="event_categories_section" style="display:none;" class="col-md-6"> 
        <form id="add_category_form" onsubmit="categoryButton" method="POST" action="/user/categories/<%=user._id%>"> 
         <label for="input_category_name">Add a category for your Events</label> 
         <div class="input-group"> 
          <div class="input-group-btn"> 
           <input type="submit" id="add_category_button" value="Add Category!" class="btn btn-primary dropdown-toggle"></input> 
          </div> 
          <input class="form-control" id="input_category_name" name="user[category]" placeholder="Category Name" required type="text" minlength="2" data-id="<%= user._id %>"> 
         </div> 
        </form> 
        <hr> 
        <div class="col-md-6"> 
         <h3>List of Categories</h3> 
         <section class="well" id="list_category_section"> 
          <% user.categories.forEach(function(category){ %> 
          <p><%= category %></p> 
          <% }); %> 
         </section> 

        </div> 
       </section> 
+0

try〜catch文でajax呼び出し部分を折り返してエラーになります。 – modernator

答えて

0

使用return falseあるご.submit()関数の最後にjQueryのイベントハンドラ内から

戻りfalseが効果的に両方event.preventDefaultを呼び出すのと同じで、渡されたjQuery.Eventオブジェクトのevent.stopPropagation。

event.preventDefault()は、デフォルトイベントの発生を防止します。

event.stopPropagation()は、イベントのバブリングを防止します。

+0

それは動作しませんでした。私のページは空白になり、提出後はアドレスバーに投稿要求のURLだけがあります。 – illcrx

1

更新あなたのコードとチェック

<section id="event_categories_section" style="display:none;" class="col-md-6"> 
        <form id="add_category_form" method="POST" action="/user/categories/<%=user._id%>"> 
         <label for="input_category_name">Add a category for your Events</label> 
         <div class="input-group"> 
          <div class="input-group-btn"> 
           <input type="submit" id="add_category_button" value="Add Category!" class="btn btn-primary dropdown-toggle"></input> 
          </div> 
          <input class="form-control" id="input_category_name" name="user[category]" placeholder="Category Name" required type="text" minlength="2" data-id="<%= user._id %>"> 
         </div> 
        </form> 
        <hr> 
        <div class="col-md-6"> 
         <h3>List of Categories</h3> 
         <section class="well" id="list_category_section"> 
          <% user.categories.forEach(function(category){ %> 
          <p><%= category %></p> 
          <% }); %> 
         </section> 

        </div> 
       </section> 

Ajaxのセクションでは、: -

$(function() { 
$("#add_category_form").submit(function (event){ 
    event.preventDefault(); 

    $.ajax({      //this gets the ID, you have to add a data-id="user._id" to the input you want to send the request from. 
     url: $("add_category_form").attr("action"), 
     type: "POST", 
     dataType: json, 
     done: function (result) { 
      alert("successful"); 
      //console.log(result.categories); 
     }, 
     fail: function (fail){ 
      console.log(fail); 
     } 
    }); 
}); 
}); 
+0

なぜEJSタグをすべて削除したのか分かりません。 – illcrx

+0

申し訳ありませんが、フォームから 'onsubmit = "categoryButton"のみを削除し、jqueryを害して、 –

0

フォームからをonSubmit属性を削除し、(categoryButtonを呼び出します)がドキュメントの準備ができたら機能し、送信イベントはフォームとバインドされ、すべて正常に動作します。 私はあなたのコードをちょっと修正しました。試してみてください。

$(function() { 
    categoryButton(); 
    }) 
var categoryButton = function(){ 
$("#add_category_form").submit(function (event){ 
    event.preventDefault(); 

    $.ajax({      //this gets the ID, you have to add a data-id="user._id" to the input you want to send the request from. 
     url: $("add_category_form").attr("action"), 
     type: "POST", 
     dataType: json, 
     done: function (result) { 
      alert("successful"); 
      //console.log(result.categories); 
     }, 
     fail: function (fail){ 
      console.log(fail); 
     } 
    }); 
}); 
}; 
+0

を確認してください。これですべてが壊れました。ページがリロードされず、送信ボタンが押されているだけです......この進行はありますか? – illcrx

+0

ああ申し訳ありません..これは動作するかもしれない –

+0

私の上記のコードを忘れてonsubmit = "return false; categoryButton(event); htmlで –

関連する問題