【问题标题】:Http Post not sending data to another page in angular jsHttp Post没有在角度js中将数据发送到另一个页面
【发布时间】:2015-11-04 05:14:22
【问题描述】:

我正在 Angular Js 中处理登录表单。HTML 和验证部分工作正常。

问题是当我使用 HTTP POST 方法将数据发送到我的服务调用 PHP 页面并希望将其保存到数据库时。

我已经尝试了通过在JS中设置这个

headers: {'Content-Type': 'application/x-www-form-urlencoded'}

我已尝试将表单的 enctype 设置为

enctype="application/x-www-form-urlencoded" 

也一样。但是它们都不起作用,我无法通过 $_REQUEST、$_POST、$_GET 中的任何一种方法获取数据。

我也尝试过使用 PHP 库函数。

$postdata = file_get_contents("php://input");

但它给出了一些我无法处理的奇怪字符串,因为 POST 数据的数量可能是数百个。

所以这还有其他方法可以解决这个问题。

http 请求的代码是

var req = {
    method: 'POST',
    url: 'servicecall.php',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    data: { 
            username: $scope.loginData.username,
            password: $scope.loginData.password,
            action : 'checkLogin'
        }  //First way of sending data

//Second way of sending data which is also not working
//data: "{username:"+$scope.loginData.username+",password:"+$scope.loginData.password+",action:checkLogin}" 

}  

$http(req).then(function(response){
        console.log(response);  
   }, function(response){
        alert("Error "+response);
   });

在 PHP 页面我做

print_r($_REQUEST);
print_r($_POST);

但它只打印像array{}这样的空白数组

【问题讨论】:

  • 我们能看到完整的$http({}) 对象吗?我怀疑在将数据发送到服务器之前如何格式化数据存在问题。
  • 请提供$http调用的代码。好像有问题。
  • 你能添加代码片段吗
  • @Ohgodwhy 我也添加了 http 代码。
  • 您在控制台中得到了什么?

标签: php angularjs http post


【解决方案1】:

遵循我用于相同目的的代码。希望对您有所帮助。

var app = angular.module('angularApp', []);

app.controller('loginCtrl', function ($scope, $http) {

    $scope.login = function () {

            var request = $http({
                method: "post",
                url: "login.php",
                data: {
                    email: $scope.email,
                    password: $scope.password
                },
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            });
            /* Successful HTTP post request or not */
            request.success(function (data) {
                if(data == "1"){
                    $scope.responseMessage = "Successfully Logged In";
                }
                else {
                    $scope.responseMessage = "Username or Password is incorrect";
                }
            });
    }
});

【讨论】:

  • 输出与我的相同。
猜你喜欢
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
  • 2020-08-16
  • 1970-01-01
  • 2015-03-11
  • 2017-11-03
  • 2013-01-24
  • 1970-01-01
相关资源
最近更新 更多