问题描述
我正在用Java开发一个网站用于后端,而使用Angular开发一个网站.在某些情况下,某些外部网站可能会使用POST表单将数据发送到我的网站.例如,
▼常规
请求URL: https://myangularwebsite/
请求方法: POST
...
▼请求标头
Content-Type: application/x-www-form-urlencoded
主持人: myangularwebsite
来源: https://externalwebsite
引荐来源: https://externalwebsite/send.form?id=0
...
▼表单数据
ID: 0000000
时间::2017.06.04 11:53:58
签名: ... ... geirgmGKFGJWR ...
...
现在,我需要以某种方式在Angular中捕获表单,将其发送/重定向到后端以验证签名,并将答案返回Angular以继续使用此网站.我尝试将其发布到我的网站上以测试如何使用Postman进行工作,但获得Cannot POST /
.
我知道如何在Angular中使用GET和URL查询参数,但我认为我需要根据从externalwebsite
到myangularwebsite
时从Chrome DevTools网络"部分看到的标头处理POST请求./p>
我应该在后端指定一条路由,然后在后端公开它,例如.../api/external
,并告诉这些网站使用此链接,而不是直接发布到Angular网站的主页吗?
我已经阅读了另一个问题(如何读取Angular 2打字稿中的表单发布数据?),这有点相似,但是我认为使用PHP不是适合我的正确方法,因为我正在开发的网站已经有使用PHP编写的较旧版本.
您提供的链接上的答案是正确的:您不能仅使用Javascript来执行此操作,而必须使用一些服务器端代码.他们以PHP为例,但是任何服务器端组件都可以,并且在后端使用Java的情况下,也请使用Java.
因此,当HTTP请求来自外部站点时,您必须使用服务器端组件来处理它.但是有一些选择.
-
如果此请求是使用您的用户浏览器发出的(因此类似于重定向,但使用POST方法),则可以执行以下操作:在后端捕获该请求,并输出带有某些内容的JavaScript数据发送到用户的浏览器并在Angular代码中处理该数据.或者这可以重定向到您的主要Angular入口点,这取决于您.
-
如果此请求是通过其他方式发出的(例如,这是服务器到服务器的请求,通过curl发出,例如来自信用卡处理的通知),则不涉及浏览器,那么不需要任何Javascript(Angular或其他语言),因为仅浏览器才需要.在这种情况下,您只需在服务器端处理请求即可.
在两种情况下,似乎都应该专门指定一些特殊的端点来处理(或着陆)此类外部请求.
I am developing a website with Java for the backend and Angular for frontend. There is a situation when some external websites may send data to my website using POST form. For instance,
▼ General
Request URL: https://myangularwebsite/
Request Method: POST
...
▼ Request Headers
Content-Type: application/x-www-form-urlencoded
Host: myangularwebsite
Origin: https://externalwebsite
Referer: https://externalwebsite/send.form?id=0
...
▼ Form data
ID: 0000000
TIME: 2017.06.04 11:53:58
SIGNATURE: ...geirgmGKFGJWR...
...
Now, I need to capture the form in Angular somehow, send/redirect it to the backend to validate the signature and return the answer back to Angular to proceed working with this website.I tried posting to my website to test how it might work using Postman, but get Cannot POST /
.
I know how to work with GET and URL query parameters in Angular but I think I need to process a POST request based on headers I see with Chrome DevTools 'Network' section when coming from externalwebsite
to myangularwebsite
.
Should I dedicate a route in the backend and expose it, for example, .../api/external
in my backend and tell these websites to use this link instead of directly posting to my Angular website's homepage?
I have already read another question ( How to read form post data in Angular 2 typescript? ) which is somewhat similar but I do not think using PHP is the right way for me as the website I am developing already has an older version written in PHP.
The answer at the link you provided is correct: you cannot do it in just Javascript, you have to use some server-side code. They mention PHP as an example, but any server-side component will do, and as you have Java at your backend, let it be Java.
So, when an HTTP request comes from an external site, you have to use a server-side component to handle it. But there are some options.
If this request is made using your user browser (so it is something like a redirect, but using a POST method), then you can do the following: catch that request at your backend, output some javascript with some data to the user's browser and process that data in your Angular code. Or this could be a redirection to your main Angular entry point, it is up to you.
If this request is made by some other means (for example, this is a server-to-server request made with with curl like a notification from a Credit Card processing), with no browser involved, then you don't need to have any Javascript (Angular or whatever it could be) as they are needed for browser only. In this case you just handle the request at your server-side.
In both cases, it seems plausible to dedicate some special endpoint for handling (or landing) such externally-originated requests.
这篇关于阅读外部网站提交的Angular POST表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!