2016-08-16 64 views
0

美好的一天。我試圖使用JavaScript將登錄數據(電子郵件,密碼,IP地址,日期)發佈到文件(all.txt)。這很難。請,你能幫忙嗎?用JavaScript發佈登錄數據

<head><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<script type="text/javascript"> 

function validatePass(){ 
    var password = document.getElementById('password').value; 
    if (password == "cm"){ 
     alert('Yeah, you are in!')} 
    else{ 
     alert('Give it another shot')} 
    } 

    $('#enter').on('submit', function(e){ 
     e.preventDefault(); 
     $.ajax({ 
      type: "POST", 
      url: "/all.txt", 
      data: $(this).serialize(), 
      success: function() { 
       alert('success'); 
      } 
     }); 
}); 

</script> </head> 

<form id="enter" action="http://google.com" onsubmit="return (this.password.value==='cm')?true:false;"> 
    <link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet"> 

    <input type="text" id="email" name="email" style="width: 225px; border: 0px solid #CDCDCD; color: rgba(0, 0, 0, 0.5); font-family: 'Raleway', sans-serif; font-weight: normal; font-size: 1em; background-color: #FFFFFF; text-align: center;" onblur="if (this.value == '') {this.value = 'email';}" onfocus="if(this.value == 'email') {this.value = '';}" value="email"> 
    <p> 
     <input type="password" id="password" name="password" style="width: 225px; border: 0px solid #CDCDCD; color: rgba(0, 0, 0, 0.5); font-family: 'Raleway', sans-serif; font-weight: normal; font-size: 1em; background-color: #FFFFFF; text-align: center;" onblur="if (this.value == '') {this.value = 'password';}" onfocus="if(this.value == 'password') {this.value = '';}" value="password"> 
    <p> 
    <input type="submit" value="log" name="log" onclick="validatePass();" style="margin: 40px 40px 40px 90px; color: #ca0000; border: 0px; background-color: rgba(51, 51, 51, 0); font-family: 'Raleway', sans-serif; font-weight: normal; font-size: 1em;" > 

</form> 
+0

「這很難」不是一個有用的問題。你有什麼問題? – Barmar

+0

你不應該在Javascript中檢查密碼,這應該在服務器上完成。 – Barmar

+1

您無法發佈到文本文件。 'POST'需要將數據提交給服務器上運行的腳本。腳本可以寫入文件。 – Barmar

回答

0
$('#enter').on('submit', function(e){ 
     e.preventDefault(); 
     $.ajax({ 
      type: "POST", 
      url: "**/validateUser**", 
      data: $(this).serialize(), 
      success: function() { 
       alert('success'); 
      } 
     }); 

而是在網址調用名爲.txt,您還可以從服務器端Web方法或操作方法。在該方法中,您可以驗證您的用戶標識和密碼,並返回成功或失敗。

如果您使用.net MVC,您可以在服務器端創建一個操作方法。

+0

這對我的情況也非常有用;經過大量的研究。謝謝Suyesh。 – NickDimou

0

HTTP沒有文件的概念。當您發佈到URL時,您正在向服務器發送一些數據(在這種情況下是從表單生成的查詢字符串),並指出該數據用於URL中的數據。由服務器端代碼決定如何處理您的請求(根據您提供的URL和數據執行某些操作)。我建議您打開瀏覽器的網絡日誌,查看實際發送的請求數據。

+0

很明顯,當我提出這個問題時,我並不知道你的有效評論。我在研究解決方案時考慮了他們。非常讚賞csander。 – NickDimou

0

可以按如下方式使用XMLHttpRequest對象:

xhr.open("POST", url, true); 
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); 
xhr.send(someStuff);