【问题标题】:How can I send this request to another host?如何将此请求发送到另一台主机?
【发布时间】:2021-07-14 09:24:52
【问题描述】:

当我在我的服务器上运行以下代码并单击按钮时,它会将请求发送到我自己的服务器,

这是请求标头

GET /dir/api/blah/confirmation HTTP/1.1
Host: myserver.com
User-Agent: Mozilla/5.0
Content-type: application/x-www-form-urlencoded
Accept: */*
Referer: http://myserver.com/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Connection: close

但我想要发送请求另一个网站,例如example.com/dir/api/blah/confirmation

我该怎么做?

这是代码

<!DOCTYPE html>
<html>
<body>

<form action="https://example.com" method="POST">
<button type="button" onclick="loadDoc()">Request data</button>

<p id="demo"></p>
 
<script>
function loadDoc() {
  const xhttp = new XMLHttpRequest();
  xhttp.onload = function() {
    document.getElementById("demo").innerHTML = this.responseText;
  }
  xhttp.open("GET", "confirmation");
  xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhttp.send("fname=Henry&lname=Ford");
}
</script>
</form>

</body>
</html>

【问题讨论】:

    标签: javascript html xml header httprequest


    【解决方案1】:

    尝试在 open 方法中使用完整的 url:

    var url = 'example.com/dir/api/blah/confirmation';
    xhttp.open('GET', url)
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.send("fname=Henry&lname=Ford");
    

    小心 CORS: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

    【讨论】:

    • 它改变目录但不改变主机。 GET /example.com/dir/api/blah/confirmation HTTP /1.1 Host: myserver.com
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 2019-05-19
    • 2023-04-05
    • 2015-03-25
    • 2017-04-30
    • 1970-01-01
    相关资源
    最近更新 更多