gpt4 book ai didi

javascript - 从 HTML 脚本向另一个域中的 Node.JS 应用程序发出 POST JSON 请求

转载 作者:行者123 更新时间:2023-12-03 04:04:15 25 4
gpt4 key购买 nike

我正在尝试通过 .erb.html 文件内的 javascript 发出 post json 请求,将字符串数据发送到另一个域中的 node.js 应用程序,该应用程序使用 Express 来处理传入请求。

我从几篇文章中读到,仅使用 XMLHttpRequest 是不可能做到这一点的。如何向这样的应用程序发送这样的请求?

最佳答案

要使用 XMLHttpRequest 执行此操作,远程服务器必须配置 CORS header 。

From MDN :

A resource makes a cross-origin HTTP request when it requests a resource from a different domain, protocol, or port to its own. For example, an HTML page served from http://domain-a.com makes an src request for http://domain-b.com/image.jpg. Many pages on the web today load resources like CSS stylesheets, images, and scripts from separate domains.

For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts. For example, XMLHttpRequest and Fetch follow the same-origin policy. So, a web application using XMLHttpRequest or Fetch could only make HTTP requests to its own domain. To improve web applications, developers asked browser vendors to allow cross-domain requests.

关于javascript - 从 HTML 脚本向另一个域中的 Node.JS 应用程序发出 POST JSON 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44629350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com