我正在尝试在本地主机上调用Restful服务。我这样做是因为这是一个异步调用。调用我的服务的相应网址以及网址模板是:
“ http://localhost:65016/Service1.svc/SN?lower=200&upper=300”
在我尝试打开(xhttp.open)的行上,每当我像下面这样实际插入url时,我的客户端页面就只接收正确的数据:
xhttp.open("GET", "http://localhost:65016/Service1.svc/SN?lower=200&upper=300" , true);
但是我需要200和300数字作为用户输入,因此我尝试了以下两件事:我首先尝试获取用户输入并将其简单地连接到URi模板之间的基本URL,如下所示:
<script>
function ServiceCall()
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function ()
{
if (xhttp.readyState == 4 && xhttp.status == 200) {
var ans = document.getElementById("secretNum");
ans.innerHTML = xhttp.responseText;
}
}
var base_uri = "http://localhost:65016/Service1.svc/";
// grab the lower number
var ln = document.getElementById("LN").firstChild;
var LN = ln.nodeValue;
// grab upper number
var un = document.getElementById("UN").firstChild;
var UN = un.nodeValue;
//complete
var URL = base_uri + "SN?lower=" + LN + "&upper=" + UN;
xhttp.open("GET", URL, true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send();
}
</script>
不起作用。因此,我尝试查看xmlHttpRequest.open的文档,然后看到该参数必须是URL。因此,我尝试使用URL(string)函数并将输出用作参数,但该方法也不起作用。有什么帮助吗?
谢谢。我帮助查看了网络请求。我只是在使用错误的语法来获取html input标签内部的值。
var ln = document.getElementById("LN").value;
返回用户输入给定的html input标签内部的实数值。我正在回答自己的问题,因为这是一项家庭作业。(不是我在作弊。回答这个问题远不能解决作业)