2016-07-04 13 views
0

私はEJSを使用してマイページに値を送信しています。以下はコードです。値をHTMLページに戻すことができません

app.post('/ttt', function (req,res){ 
    res.render('index.ejs', {titles: 'CAME IN'}) 
}); 

HTML

<form id="mc-form" action="http://local_host:8081/ttt" method="post" enctype="multipart/form-data"> 
    <input type="email" value="" name="dEmail" class="email" id="mc-email" placeholder="type email &amp; hit enter" required=""> 
    <input type="submit" name="subscribe" > 
    <label>REEEEE <%= titles %></label> 
</form> 

しかし、送信ボタンをユーザーがクリックした後、テキストCAME INは、フォーム上に表示されていないばかり

<script type="text/javascript"> 
$("#mc-form").submit(function(e) { 
    // e.preventDefault(); // Prevents the page from refreshing 
    var $this = $(this); // `this` refers to the current form element 
    $.post(
     $this.attr("action"), // Gets the URL to sent the post to 
     $this.serialize(), // Serializes form data in standard format 
     function(data) { /** code to handle response **/ 
      alert(data); 
     }, 
     "json" // The format the response should be in 
    ); 
}); 

JS

。これをどのようにソートできますか?

+0

ページ – madalinivascu

+0

にデータ(HTML)を追加されていませんどうやってやるの。私は初心者です – Illep

答えて

3

戻るAJAXないビューのためのJSON

のNode.js

app.post('/ttt', function (req,res){ 
    res.setHeader('Content-Type', 'application/json'); 
    res.send(JSON.stringify({titles: 'CAME IN'})); 

}); 

plain.js

$("#mc-form").submit(function(e) { 
    e.preventDefault(); // Prevents the page from refreshing 
    var $this = $(this); // `this` refers to the current form element 
    $.post(
     $this.attr("action"), // Gets the URL to sent the post to 
     $this.serialize(), // Serializes form data in standard format 
     function(data) { /** code to handle response **/ 
      $this.find('label').text(data.titles);//append the title to the form 
     }, 
     "json" // The format the response should be in 
    ); 
}); 
+0

ラベルはまだ更新されませんか?私はIDを紛失していますか?それとも? – Illep

+0

コンソールにエラーがありますか? – madalinivascu

+0

コンソールにエラーはありません – Illep

関連する問題