Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中随表单post发送标题数据_Php - Fatal编程技术网

如何在PHP中随表单post发送标题数据

如何在PHP中随表单post发送标题数据,php,Php,在表单提交时,通过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

在表单提交时,通过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目标仅在发送授权标头时才接受数据。

您应该使用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>
名字:
姓氏:
提交
您有权访问您正试图向其发送此请求的其他域吗?有。我有访问权限。这是HTML无法做到的。我想这可以通过JavaScript和AJAX实现。为什么不将授权数据作为表单字段发送呢?@Robert您通常不会选择如何使用第三方API。对于这个任务,使用HTTP头是相当标准的。实际出现的问题是,为什么要直接从常规HTML表单查询API。