我想采用HTML表单的字段之一,并使用它来修改操作的URL.我遵循了this question,它显示了如何处理GET请求,但是我在修改POST请求的技术时遇到了麻烦.
User location lookupfunction process()
{
var url="http://localhost:43/" + document.getElementById("url").value;
location.href=url;
return false;
}
Username:
function process2()
{
var url="http://localhost:43/" + document.getElementById("url").value;
location.href=url;
return false;
}
Username:
Location:
GET请求正确发送:
GET /brian HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-GB
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: localhost:43
DNT: 1
Connection: Keep-Alive
而对于POST我得到:
GET /brian HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-GB
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: localhost:43
DNT: 1
Connection: Keep-Alive
即使方法是POST,也仍然是GET.
我想生成的是:
POST /brian HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-GB
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: localhost:43
Content-Length: 11
DNT: 1
Connection: Keep-Alive
location=SO
有人可以用尽可能少的JavaScript代码为我指明正确的方向吗?
解决方法:
您的代码未提交表单.它实际上是通过GET直接调用URL,即:此方法仅适用于GET.
请试试:
Username:
Location:
function updateAction() {
var url = document.getElementById("url2").value;
document.getElementById("myformid").setAttribute("action", "http://localhost:43/" + url);
}
标签:http-post,html,javascript
来源: https://codeday.me/bug/20191026/1940337.html