【问题标题】:how to navigate from angular to php page after $http.post()$http.post() 之后如何从角度导航到 php 页面
【发布时间】:2016-02-14 07:55:59
【问题描述】:

现在我有一个由 Angular js 控制器管理的 php 页面“userreq.php”。单击按钮我正在调用一个函数,它将数据发布到另一个 php 页面“checkout.php”。

$http({
        url: "checkout.php",
        method: "POST",
        data: {
            data: variable
        }
    }).success(function(response) {
    console.log(response);
});

这就是我发布它的方式。现在我希望页面在发布变量后导航到 checkout.php,以便我可以在 checkout.php 中使用该变量。我怎样才能做到这一点。

【问题讨论】:

  • 你可以使用 window.location.href = 'url';在您的成功功能中。 developer.mozilla.org/en-US/docs/Web/API/Window/location
  • 但它是重定向正确的 http post 将没有效果..我是 angular 新手,所以我可能错了
  • 取决于你想要什么,你可以简单地用 window.location 传递参数而不是使用 ajax
  • $scope.checkout = function(){ $http({ url:"checkout.php", method : 'POST', data :{ total:$scope.total }, headers : {' Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'} }).success(function(data, status, headers, config) { window.location.href="checkout.php" ; }).error(function(data, status, headers, config) { console.log("not done"); });并在 checkout.php 我正在做 但我得到的错误是未定义总数
  • 感谢先生的帮助..但实际上我需要传递一个数组

标签: javascript php angularjs http


【解决方案1】:

在您的控制器中添加$httpParamSerializerJQLike 并将您的数据作为查询参数传递。

例子:

var url = 'checkout.php'; 
var data = { id: 1, name: 'foo' };

$http({
    url: url,
    method: "POST",
    data: data
})
.success(function(response) {
    console.log(response);
    window.location.href = url + '?' + $httpParamSerializerJQLike(data);
});

然后在您的 php 中,从 $_GET 检索数据:

$field = $_GET[$field];
// Output: array( 'id' => 1, 'name' => 'foo' )

【讨论】:

猜你喜欢
  • 2018-06-25
  • 2016-08-29
  • 2022-07-12
  • 1970-01-01
  • 2019-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-09
相关资源
最近更新 更多