【发布时间】:2023-03-05 07:20:01
【问题描述】:
我正在尝试在我的本地主机上调用 Restful 服务。我这样做是因为它是一个异步调用。调用我的服务的适当 Url 加上 Uri 模板是这样的:
“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) 函数并将输出用作参数,但这也不起作用。 请问有什么帮助吗?
【问题讨论】:
-
使用浏览器中的调试工具。查看正在发出的网络请求。并 console.log 记录 URL 变量以查看其真实值。