从HTML脚本向另一个域中的Node.JS应用发出POST JSON请求 [英] Make POST JSON Request From HTML Script To A Node.JS App In Another Domain

查看:116
本文介绍了从HTML脚本向另一个域中的Node.JS应用发出POST JSON请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to send string data via making a post json request by javascript inside an .erb.html file, to a node.js app in another domain, which uses express to handle incoming requests.

我从几篇文章中读到,仅靠XMLHttpRequest不可能实现这种事情.如何将这样的请求发送到这样的应用程序?

I read from several posts that such thing with XMLHttpRequest alone is not possible. How one sends such request to such an app?

推荐答案

要对XMLHttpRequest进行此操作,远程服务器将必须配置CORS heaer.

To do it with an XMLHttpRequest, the remote server would have to have CORS heaers configured.

来自MDN :

资源在请求资源时发出跨域HTTP请求 来自不同域,协议或端口的资源.为了 例如,从 http://domain-a.com 提供的HTML页面 src请求 http://domain-b.com/image.jpg .网络上的许多页面 今天从中加载CSS样式表,图像和脚本等资源 单独的域.

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.

出于安全原因,浏览器限制跨域HTTP请求 从脚本内部启动.例如,XMLHttpRequest和Fetch 遵循原产地政策.因此,使用 XMLHttpRequest或Fetch只能对自己的HTTP请求 领域.为了改善Web应用程序,开发人员要求浏览器供应商 允许跨域请求.

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.

这篇关于从HTML脚本向另一个域中的Node.JS应用发出POST JSON请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆