2017-01-20 123 views
-1

我有下面的ajax調用,我試圖將註冊數據直接發佈到URL,但無論如何我可以存儲這些變量,然後將其傳遞到URL中。請提前幫助謝謝你。在AJAX中傳遞變量到URL

你可以看到的script.js這個plunker

的jQuery:

$(function() { 

    /* Registration form for the website */ 
    /* validation */ 
    $("#register-form").validate({ 
     rules: { 
      firstName: { 
       required: true 
      }, 
      lastName: { 
       required: true 
      }, 
      userName: { 
       required: true, 
       minlength: 4 
      }, 
      email: { 
       required: true, 
       email: true 
      }, 
      password: { 
       required: true, 
       minlength: 8, 
       maxlength: 15 
      }, 
      cpassword: { 
       required: true, 
       equalTo: '#password' 
      }, 
     }, 
     messages: { 
      userName: "please enter a valid user name", 
      password: { 
       required: "please provide a password", 
       minlength: "password at least have 8 characters" 
      }, 
      email: "please enter a valid email address", 
      cpassword: { 
       required: "please retype your password", 
       equalTo: "password doesn't match !" 
      } 
     }, 
     submitHandler: submitForm 
    }); 
    /* validation */ 

    /* form submit */ 
    function submitForm() { 
     var data = $("#register-form").serialize(); 
     // var data={ 
     // firstName: $('#firstName').val(), 
     // } 

     $.ajax({ 

      url: 'http://localhost:8000?userName=&password=&firstName=&lastName=&email=', 
      type: 'POST', 
      data: data, 

      beforeSend: function() { 
       $("#error").fadeOut(); 
       $("#btn-submit").html('<span class="glyphicon glyphicon-transfer"></span> &nbsp; sending ...'); 
      }, 

      success: function(data) { 
       if (data === 0) { 

        $("#error").fadeIn(1000, function() { 


         $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; Sorry email already taken !</div>'); 

         $("#btn-submit").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; Create Account'); 

        }); 

       } else if (data == 1) { 

        $("#btn-submit").html('<img src="btn-ajax-loader.gif" /> &nbsp; Signing Up ...'); 

       } else { 

        $("#error").fadeIn(1000, function() { 

         $("#error").html('<div class="alert alert-danger"><span class="glyphicon glyphicon-info-sign"></span> &nbsp; ' + data + ' !</div>'); 

         $("#btn-submit").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; Create Account'); 

        }); 

       } 
      } 
     }); 
     return false; 
    } 
}); 
+0

可能的重複[如何檢查數據是否通過ajax](http://stackoverflow.com/questions/41766927/how-to-check-if-data-is-passed-in-ajax) –

+0

如果你想把變量放到URL中,然後使用'GET'類型的請求。 – Black

+0

@Leo這是一個重複我同意,但這是我有不同的問題。我能夠看到數據,但沒有正確傳遞給URL,出於某種原因,導致所有輸入的'逗號'。我認爲這是因爲我已經將它硬編碼到URL本身,而不是存儲每個單獨的組件,然後將它傳遞給URL。你能幫助一個例子嗎? – Bob

回答

0

你的AJAX網址是錯誤的,你不需要輸入在完整的地址。 Serialize方法應該爲你做到這一點。所有你需要的是localhost:8000? + data。正如@ zer00ne所說的,如果您使用密碼發送郵件,則不會使用GET,它會顯示在URL上。作爲一個經驗法則,表單中發送的任何內容應該是POST請求,除非我不知道的一些罕見情況。

我的建議:

$.ajax({ 

     url: 'http://localhost:8000?', 
     type: 'POST', 
     data: data, 

這樣你發送你的信息傳遞到正確的地方和安全。

1
url: 'http://localhost:8000?userName=&password=&firstName=&lastName=&email=', 
    type: 'POST', 
    data: data, 

成爲

url: 'http://localhost:8000?' + data, 
    type: 'GET', 
    //data: data, 
+0

,因爲我們不希望我們的密碼對像zer00ne所說的任何人都可見。所以我會堅持利奧的解決方案。 – Bob