javascript - 我可以将以下代码发布到任何其他域吗?

标签 javascript ajax cors xmlhttprequest same-origin-policy

是否可以从 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);

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

最佳答案

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

关于javascript - 我可以将以下代码发布到任何其他域吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55216553/

相关文章:

jquery - 使用ajax和java servlet上传文件

elasticsearch - 更新CORS的配置文件后,Elasticsearch无法重新启动

node.js - React Native/Node API : Can't set headers after they are sent

javascript - FancyBox 3 在链接点击时加载 iframe 不起作用

javascript - 在 head 中或在打开 body 标签后立即包含 JS

javascript - 为什么 Angular JS 的行为不同?

javascript - 如何在不离开页面的情况下将表单插入mysql(javascript+html)

JavaScript catch 参数已定义

java - 如何使用sx :autocomplter in Struts2 for AJAX purpose?

java - 在飞行前响应中,Access-Control-Allow-Methods 始终不允许方法删除