我有一个正在运行的服务器,该服务器接受JSON形式的POST
请求。现在,我尝试创建一个简单的html页面,一旦用户单击POST
按钮,该页面就会触发Submit
请求。我的代码如下
<!DOCTYPE html>
<html>
<br/><br/><br/><br/>
<body>
<script>
function submitform (form) {
var object = {};
form.forEach((value, key) => {object[key] = value});
var json = JSON.stringify(object);
return json;
}
</script>
<form action="http://localhost:9000/parse" method="post">
<h3>Enter url :</h3>
<input type="text" name="url" placeholder="http://google.com" style="width: 300px"><br>
<br/><br/>
<input type="submit" name="Get Website Statistics" style="width: 20em; height: 2em;" onclick="submitform()">
</form>
</body>
</html>
我希望服务器接收带有有效负载的请求
{
"url" : "https://www.example.com"
}
但是,我从服务器收到的响应为
For request 'POST /parse' [Expecting text/json or application/json body]
我的卷曲请求如下
curl 'http://localhost:9000/parse' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Origin: http://localhost:63342' -H 'Upgrade-Insecure-Requests: 1' -H 'Content-Type: application/x-www-form-urlencoded' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36' -H 'Sec-Fetch-User: ?1' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3' -H 'Sec-Fetch-Site: same-site' -H 'Sec-Fetch-Mode: navigate' -H 'Referer: http://localhost:63342/html-parser/views/index.html' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-GB,en;q=0.9,en-US;q=0.8' -H 'Cookie: Idea-836ae252=9c616505-7e0e-4a1e-bdd0-fa54e473c071' --data 'url=https%3A%2F%2Fwww.spiegel.de%2Fmeinspiegel%2Flogin.html&Get+Website+Statistics=Submit' --compressed
有人可以指导我在这里做错什么吗? TIA
答案 0 :(得分:0)
显然您没有将JSON包含在正文中
<form action="localhost:9000/parse" method="post">
<h3>Enter url :</h3>
<input type="text" name="url" placeholder="http://google.com" style="width: 300px"><br>
<br/><br/>
<input type="submit" name="Get Website Statistics" style="width: 20em; height: 2em;">
</form>
答案 1 :(得分:0)
<form action="http://localhost:9000/parse" method="post" onsubmit="return submitform()">
<input type="submit" name="Get Website Statistics" style="width: 20em; height: 2em;">
</form>
在表单标签内放置onsubmit事件。