如何从包含JSON数据(不是表单数据)的浏览器发送POST

时间:2021-09-06 22:10:38

From a webpage, I need to send JSON data using POST method to a different domain.

在网页中,我需要使用POST方法将JSON数据发送到其他域。

I can use a form with hidden fields, but then POST data is application/x-www-form-urlencoded instead of application/json, and is "name=value" instead of "value"

我可以使用带有隐藏字段的表单,但POST数据是application / x-www-form-urlencoded而不是application / json,并且是“name = value”而不是“value”

I can use XMLHttpRequest (directly or through any API) to send arbitrary JSON data in the POST payload, but then I run into cross-domain limitation.

我可以使用XMLHttpRequest(直接或通过任何API)在POST有效负载中发送任意JSON数据,但后来我遇到了跨域限制。

Also, I need the browser to really navigate to the new url, because what we are really doing is to invoke a different application sending some parameters.

此外,我需要浏览器真正导航到新的URL,因为我们真正做的是调用发送一些参数的不同应用程序。

Any options?

任何选择?

1 个解决方案

#1


2  

If you are limited to the browser, then no - there are no other options. Browsers cannot perform cross-domain POST requests to arbitrary domains. This is a security feature.

如果你只限于浏览器,那么没有 - 没有其他选择。浏览器无法对任意域执行跨域POST请求。这是一项安全功能。

If you have full control over the server (or if it's already configured for this), you might consider CORS, but this feature is not strongly supported yet.

如果您可以完全控制服务器(或者已经为此配置了服务器),您可能会考虑使用CORS,但尚未强烈支持此功能。

#1


2  

If you are limited to the browser, then no - there are no other options. Browsers cannot perform cross-domain POST requests to arbitrary domains. This is a security feature.

如果你只限于浏览器,那么没有 - 没有其他选择。浏览器无法对任意域执行跨域POST请求。这是一项安全功能。

If you have full control over the server (or if it's already configured for this), you might consider CORS, but this feature is not strongly supported yet.

如果您可以完全控制服务器(或者已经为此配置了服务器),您可能会考虑使用CORS,但尚未强烈支持此功能。