簡體   English   中英

這是發送GET請求URL的正確語法嗎?

[英]Is this the correct syntax to send a GET request URL?

為了將值插入到我的表中,我嘗試了此GET xmlhttprequest對象。 URL中的語法正確嗎? 沒用

document.getElementById('allsubmit').addEventListener('click',sendPost);
  var com = document.getElementById('inputcompany').value;
  var cat = document.getElementById('selectCategory').value;
  var subcat = document.getElementById('selectsubCategory').value;
  var descrip = document.getElementById('textdescription').value;
  var exp = document.getElementById('datepicker').value;

  function sendPost() {
   var xhr = new XMLHttpRequest();
    xhr.open('GET',"addingthevacancy.php?company='"+com+"'?category='"+cat+"'?subcategory='"+subcat+"'?description='"+descrip+"'?expdate='"+exp,true);

xhr.onprogress = function() {
      //
}

xhr.onload = function() {
    console.log("Processed..."+xhr.readystate);
    console.log(this.responseText);
}

xhr.send();
}

我不知道這是怎么回事。

幾個問題:

  1. 參數必須用&分隔,不能?
  2. URL參數不需要用引號引起來。
  3. 參數應使用encodeURIComponent()進行編碼。
  4. 您需要在sendPost()函數中獲取輸入的值; 您的代碼是在頁面首次加載時設置變量,而不是在用戶提交時設置變量。
  5. 如果該按鈕是一個提交按鈕,則需要調用e.preventDefault()來覆蓋默認的提交。

通常不建議將GET用於在服務器上進行更改的請求,通常應將POST用於這些類型的請求。 瀏覽器緩存GET請求,因此,如果您確實需要執行此操作,則應添加一個cache-buster參數(一個額外的未使用的參數,該參數包含每次更改的隨機字符串或時間戳,只是為了防止URL與緩存的URL匹配) 。

 document.getElementById('allsubmit').addEventListener('click', sendPost); function sendPost(e) { e.preventDefault(); var com = encodeURIComponent(document.getElementById('inputcompany').value); var cat = encodeURIComponent(document.getElementById('selectCategory').value); var subcat = encodeURIComponent(document.getElementById('selectsubCategory').value); var descrip = encodeURIComponent(document.getElementById('textdescription').value); var exp = encodeURIComponent(document.getElementById('datepicker').value); var xhr = new XMLHttpRequest(); xhr.open('GET', "addingthevacancy.php?company=" + com + "&category='" + cat + "&subcategory=" + subcat + "&description=" + descrip + "&expdate=" + exp, true); xhr.onprogress = function() { // } xhr.onload = function() { console.log("Processed..." + xhr.readystate); console.log(this.responseText); } xhr.send(); } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM