2016-05-02 60 views
0

我有一個表格:如何在節點中爲用戶創建某種警報?

<form class="form-inline" action="/" method="post"> 
    <div class="form-group"> 
     <label class="sr-only">Username</label> 
     <input name="username" type="text" class="form-control" placeholder="Desired Username"> 
    </div> 
    <br> 
    <br> 
    <div class="form-group"> 
     <label class="sr-only" for="exampleInputEmail3">Email address</label> 
     <input name="email" type="email" class="form-control" id="exampleInputEmail3" placeholder="Email"> 
    </div> 
    <br> 
    <br> 
    <div class="form-group"> 
     <label class="sr-only" for="exampleInputPassword3">Password</label> 
     <input name="password" type="password" class="form-control" id="exampleInputPassword3" placeholder="Password"> 
    </div> 
    <br> 
    <br> 
    <button type="submit" class="btn btn-default">Register</button> 
</form> 

這裏是我的app.js:

app.post('/', function(req, res) { 

    var user = new Parse.User(); 
    user.set("username", req.body.username); 
    user.set("password", req.body.password); 
    user.set("email", req.body.email); 

    user.signUp(null, { 
    success: function(user) { 
     // Hooray! Let them use the app now. 
     console.log(user); 
     res.redirect('/#register'); 
    }, 
    error: function(user, error) { 
     // Show the error message somewhere and let the user try again. 
     console.log(error.message); 
     res.redirect('/#register'); 

    } 
    }); 

}); 

一切功能的作品,我只是想知道我怎麼可以顯示一個消息,如果用戶已經成功簽約或不是。我還想知道這是否是處理表單的正確方法。

+0

你應該叫'res.send'從那裏發送成功消息前端和處理重定向。 –

+0

你能舉個例子嗎? – user2975275

+0

檢查我的答案。 –

回答

0

處理註冊後的成功狀態。

success: function(user) { 
    // Hooray! Let them use the app now. 
    res.redirect('/#register'); 
} 

成功註冊後,您將重定向到/#註冊頁面。您可以在加載此頁面時顯示警報。

+0

我試過alert();但它不起作用 – user2975275

+0

'alert'在服務器端不存在,它只存在於瀏覽器中。 –

+0

但我的功能在服務器端。我怎麼能仍然警惕? – user2975275

0

前端JS:

而是提交表單通常的方式,我們將使用jQuery-AJAX用於此目的:


使用下面的<script>標籤附加jQuery到您的網頁在您的網頁上<head>標記:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script> 

跟着它了在同一<head>有以下幾點:

<script type="text/javascript"> 

$('.form-inline').submit(function(event) { 

    $.ajax({ 
    type: "POST", // the request type 
    url: "/", // the URL where your form data will be sent 
    data: $(this).serialize(), // the form data to be sent 
    success: function(data) { // this function will be called when you do a res.send from the back-end 

     alert(data); // although I would suggest you use a modal or some other solution to display your success message 

     window.location.href = your_redirect_url; // this will redirect the user to your /#register url 

    } 
    }); 

    event.preventDefault(); // to prevent the form from being submitted the usual way 

}); 

</script> 

注:這僅僅是一個例子,以幫助您開始。


後端JS:

 
app.post('/', function(req, res) { 

    ... 

    user.signUp(null, { 
    success: function(user) { 

     res.send(your_success_message); // send your success message or status or result object using this method, it will call the success function of your ajax object on the front-end 
    }, 
    error: function(user, error) { 

     res.send(your_error_message); // similarly send the error message, or status, or object. 
    } 
    }); 
}); 
+0

沒有Ajax可以做到這一點?像我可以做到只有簡單的節點和HTML? – user2975275

+0

如果您不想使用Ajax,那麼您將需要恢復爲本地本地的[XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest)函數在瀏覽器的Javascript中。但是Ajax使用起來更簡單。 –

+0

但是,如果您想在窗體的同一頁面上顯示警報消息,那麼就我所知,使用Ajax或XMLHttpRequest是唯一的方法。 –

相關問題