如何在AngularJS页面中接收POST请求? [英] How to receive POST request in an AngularJS page?

查看:206
本文介绍了如何在AngularJS页面中接收POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们制作了一个AngularJS应用,其中用户打开一个URL(xyz.com/booking)填写表格,然后选择一些要购买的物品.

We made a AngularJS app where user opens a URL (xyz.com/booking) fills the forms and then selects some items for purchase.

该用户单击购买"按钮后,离开该站点进入支付网关站点.成功付款后,Payment Gateway会通过在回调URL(xyz.com/booking-success)上发送POST请求来发回用户.

After that user clicks on BUY button and leaves the site for the payment gateway site. On successful payment, Payment Gateway sends back the user by sending a POST request on a Callback URL(xyz.com/booking-success).

现在的问题是,我为该回调URL配置的相应Angular页面没有打开.我在RouteProvider中配置了xyz.com/booking-success页面,但这似乎无法正常工作.

Now problem is that my corresponding Angular Page that I configured for that Callback URL is not opening. I configured xyz.com/booking-success page in RouteProvider, but that seems not to be working.

但是,如果我直接通过浏览器打开xyz.com/booking-success页面,它将被打开.

However, if I open the page xyz.com/booking-success directly via browser, it gets opened.

如何处理这种情况?任何帮助将不胜感激.

How this scenario can be handled? Any help would be greatly appreciated.

推荐答案

  1. ngRoute无法处理通过POST访问的URL,如果您在index.html上进行了重写,那么您只是在发布到index.html,而不是在路由,因此,当您配置/booking-成功,并且您通过cURL在该URL上发布,实际上是在发布到index.html.角形布线只是虚拟"的,不是真实的.像cURL或任何其他HTTP客户端之类的系统看不到它,因此它们正在发布到index.html.

  1. ngRoute can't handle URLs accessed via POST , if you have rewrite on index.html you are just posting to index.html not to you route, it does not work when you have configured /booking-success and you POST on that url via cURL you are actually posting to index.html. Routing in angular is just "virtual" not real. Systems like cURL or any other HTTP client cant see it and therefore they are posting to index.html.

从不处理来自付款/预订/angular内部任何网关的回调URL. Angular是危险的,因为您必须确保将执行回调过程. Angular不是容错的,如果出现任何javascript错误,Angular调用中的任何错误都将丢失所有回调数据,并且字面上就是搞砸了.

Never handle callback URLs from payment/booking/any gateway inside angular. Angular is dangerous because you must be sure that callback process will be executed. Angular is not fault tolerant , if there will be any javascript error, any error inside your angular call you will lost all callback data and you are literally screwed.

回调必须是REST API的一部分,并且必须实现以返回HTTP状态代码200,否则,如果使用其他任何HTTP代码,网关应知道它们必须重试调用. (例如,如果没有HTTP 200代码,skrill.com将重复执行10次回调).实际上,由于您的Web服务器将为index.html提供服务,因此AngularJS每次都会以200响应,它不在乎javascript代码内部会发生什么.

Callbacks must be part of your REST API and must be implemented to return HTTP status code 200, otherwise in case on any other HTTP code, gateway should know that they must retry call. (For example, skrill.com is repeating a callback 10 times in case of no HTTP 200 code). Actually, AngularJS will respond every time with a 200 because your web server will serve the index.html, it doesn't care what happens inside javascript code.

  1. 只有1个可通过angular处理的部分是redirect_url,通常是在成功在代理方付款/操作后重定向用户的URL.但是与此同时,回调URL仍在应用程序的较低级别上调用.重定向URL在浏览器重定向中通过GET调用,因此用户将看到它的正常显示.
  1. Only 1 part that can be handled via angular is redirect_url, usually URL that user is redirected after successful payment/action on broker side. But in the meantime the callback URL is still called on a lower level of your application. The redirect URL is called via GET inside browser redirect so user will see it as normal.

每个普通"网关都有callback_url(API的低级通知,表明操作成功,并且redirect_url,这将重定向回您的网站xyz.com/booking-success).但是它们应该分开.如果您的网关没有重定向,则别无选择,只能在新窗口中打开整个预订.并在调用回调后以某种方式通知您的前端.可以通过间隔轮询来完成.例如,10秒正在检索booking_id的实际状态(将在回调代码中设置的列is_processed (true|false)的实际状态.当状态从false更改为true时,您将通知前端(重定向用户或显示弹出窗口).您还可以实现socket.io库,该库可以处理轮询,并且在调用回调URL时,通知将立即发生.

Every "normal" gateway has callback_url (low level notification for your API that action is successful and redirect_url, that is redirecting back to your site xyz.com/booking-success). But they should be separated. If your gateway does not have redirection, you don't have a choice but to open whole booking in new window. And somehow notify your front end after callback is called. That can be done via polling with intervals. For example, 10 seconds, which is retrieving actual status of your booking_id (column is_processed (true|false) which you will set inside callback code. When status changes from false to true you will notify your front end (redirect user or show popup) . Also you can implement socket.io library which you can handle polling and your notification will be instant when callback URL is called.

这篇关于如何在AngularJS页面中接收POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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