【问题标题】:Send multiple URL parameters using ajax get request使用 ajax get 请求发送多个 URL 参数
【发布时间】:2015-03-27 06:32:04
【问题描述】:

我正在为我的项目开发 ajax 页面。在这个页面中,我想发送一个带有多个 url 参数的 ajax 请求。我想从 html 文本框中获取这个 url 参数。当我直接使用代码传递参数时,它工作正常。但是当我从 HTML 文本框中调用参数时,它不起作用。

这是我的代码

<!DOCTYPE html>
<html>
<head>

<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
 // xmlhttp.open("GET","portal.php?searccategory=Birdid&searchValue=2",true);
xmlhttp.open("GET","portal.php?searccategory="+searccategory+"&searchValue="+searchValue,true);

xmlhttp.send();
}
</script>
</head>
<body>


        <select name="searccategory1" >
                                    <option value="Species">Species</option>
                                    <option value="Birdid">ID</option>
                                    <option value="Age">Age</option>
                                    <option value="Sex">Sex</option>
                                    <option value="Location">Location</option>
                                    <option value="all">all</option>
                                </select>
         <input type="text" id="searccategory" required>
         <input type="text" id="searchValue" required>


<button type="button" onclick="loadXMLDoc()">Change Content</button>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>


</body>
</html>

【问题讨论】:

  • 从 HTML 中获取参数时会发生什么?
  • 但是当我使用 xmlhttp.open("GET","portal.php?searccategory=Birdid&searchValue=2",true);它工作

标签: html ajax parameters get


【解决方案1】:

请查看是否对您有帮助, searccategory 和 searchValue 返回 DOM 元素。如果您想获得价值,请使用searccategory.valuesearchValue.value。或document.getElementById("searccategory").valuedocument.getElementById("searchValue").value

xmlhttp.open("GET","portal.php?searccategory="+searccategory.value+"&searchValue="+searchValue.value,true);

xmlhttp.open("GET","portal.php?searccategory="+document.getElementById("searccategory").value+"&searchValue="+document.getElementById("searchValue").value,true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    相关资源
    最近更新 更多