【问题标题】:Can I a POST below code to any othes domain? [duplicate]我可以将以下代码发布到任何其他域吗? [复制]
【发布时间】:2019-08-08 12:53:48
【问题描述】:

是否可以从 JavaScript 向任何服务器发出 HTTP POST 请求 域名?

Javascript

var xhr = new XMLHttpRequest();

xhr.open("POST", 'URL linki', true);

 //Send the proper header information along with the request

 xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() { // Call a function when the state changes.

if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {

    // Request finished. Do processing here.

 }
 }
 xhr.send("foo=bar&lorem=ipsum");

 // xhr.send(new Int8Array());

 // xhr.send(document);

我想在屏幕上打印。有可能吗?

【问题讨论】:

  • 是的,只要 a) 它接受它,b) 它理解它,并且 c) 如果你想要一个响应,服务器允许 CORS
  • 你可以,但你可能不会:)
  • 为什么我不能,你能解释一下吗
  • 你需要的都在这里:ajax-cross-origin.com/how.html
  • tnx 这么多,我很好理解:)

标签: javascript ajax cors xmlhttprequest same-origin-policy


【解决方案1】:

是的,只要相关域接受请求并且服务器允许 CORS(跨域资源共享),就可以向任何域发出请求。仅当您需要响应时才需要后者,因为您正在跨来源共享资源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    相关资源
    最近更新 更多