【发布时间】:2014-11-20 17:17:13
【问题描述】:
我有一个我用 NodeJs 编写的服务器,它监听端口 1234。 我想从我的客户端 (html) 向我的服务器发送请求并等待答复。
我尝试使用 XMLHttpRequest:
var http = new XMLHttpRequest();
var url = "127.0.0.1:1234";
var params = "token=22";
http.open("post", url, true);//"https://www.google.com/search?q=asd"
http.setRequestHeader("Content-type", "text/html");
http.setRequestHeader("Content-length", 0);
http.setRequestHeader("Connection", "keep-alive");
http.onreadystatechange = function() {//Call a function when the state changes.
alert(http.responseText);
}
http.ontimeout = function()
{
alert("timeout");
}
http.timeout=10;
try
{
http.send(null);
}
catch(ex)
{
alert(ex);
}
但我总是遇到异常。原因是我不能使用自己的端口。
还有其他方法可以发送请求并得到响应吗?
【问题讨论】:
标签: html node.js client-server