用 AngularJS 中 $http 的响应替换整个 DOM(页面)

Replace entire DOM (page) with response from $http in AngularJS

我有 HTML AngualrJS 应用程序,其工作方式如下:

  1. 用户在表单中输入支付信息,表单没有设置动作属性
  2. 用户点击提交
  3. AngularJS 拦截数据,处理支付并向服务器发送请求。

这是控制器从我的 PHP 接收交易确认详细信息的地方:

this.stripeResponseHandler = function (status, response) {
...
$http ({
        method: 'POST',
        url: 'https://server/billpay/charge.php',
        data: postData  
}).then (function (response) {
        console.log (response.data);
        that.result = response.data;
        that.isSaving=1;
}, function (response) {
        console.log ("failed %s", response);
        that.isSaving=1;
})

问题:

我需要将整个现有页面(带有表单)替换为服务器返回的完整 HTML 页面(带有 CSS、HTML、JS)。

服务器returns用户可以打印的收据才备案。

基本上我不想在当前页面上只注入服务器对 DIV(或类似)的响应,而是需要从服务器响应重新呈现整个新页面。

我该怎么做?

谢谢!!

你可以这样做:

document.open()
document.write(that.result);
document.close();

但不知何故 "self erasing code":您的脚本在 close 之后消失了;

使用 (CSS, HTML, JS) 创建另一个页面,并在提交表单后重定向到包含服务器响应数据的该页面。