在表单提交时,数据通过POST方法发送。与此同时,如何将授权和其他标题数据发送到该帖子URL?
<form action="https://www.example.com/foobar" method="post">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>
此帖子URL目标仅在发送授权标头时才接受数据。
答案 0 :(得分:1)
您应该使用这样的jquery帖子来做到这一点:
function submit(){
var settings = {
"url": "https://www.example.com/foobar",
"method": "POST",
"headers": {
"content-type": "application/json",
"cache-control": "no-cache",
"token": "your token" // here you can add your token for authorization
},
"processData": false,
"data": {"fname": $("#fname").val(), "lname": $("#lname").val() } // your form data
}
$.ajax(settings).done(function (response) {
console.log(response);
});
}
您的HTML
应该看起来像这样:
First name: <input type="text" id="fname" name="fname"><br>
Last name: <input type="text" id="lname" name="lname"><br>
<button onclick="submit()">Submit</button>