[英]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();
}
我不知道這是怎么回事。
幾個問題:
&
分隔,不能?
。 encodeURIComponent()
進行編碼。 sendPost()
函數中獲取輸入的值; 您的代碼是在頁面首次加載時設置變量,而不是在用戶提交時設置變量。 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.