Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 表单未正确过帐_Javascript_Angularjs - Fatal编程技术网

Javascript 表单未正确过帐

Javascript 表单未正确过帐,javascript,angularjs,Javascript,Angularjs,我对angular应用程序表单帖子的格式有问题。下面是表单的html代码 <form class="col s8 center"> <h4> {{greeting}} </h4> <div class="row">

我对angular应用程序表单帖子的格式有问题。下面是表单的html代码

                <form class="col s8 center">
                    <h4>
                        {{greeting}}
                    </h4>
                  <div class="row">
                    <div class="input-field col s12">
                      <input id="email" type="email" class="validate" ng-model="user.email">
                      <label for="email">Email</label>
                    </div>
                    <div class="input-field col s12">
                      <input id="password" type="password" class="validate" ng-model="user.password">
                      <label for="password">Password</label>
                    </div>
                  </div>
                  <div class="row">
                      <div class="col s12">
                        <input type="submit" class="waves-effect waves-light btn pink" style="width:100%" value="Login" ng-click="login(user)">
                      </div>
                  </div>
                </form>
现在函数被正确调用,但是post数据如下

{"password":"d","email":"d@s"}:""
做同样事情的正确方法是什么?我错在哪里

**编辑**


post数据取自firefox开发工具检查器。

对于此
应用程序/x-www-form-urlencoded
内容类型,您需要将数据正确序列化为“key=vale&key1=value2”字符串。为此,您可以使用内置服务
$httpParamSerializer

$http({
  url: baseDomain + 'auth/login/',
  method: "POST",
  data: $httpParamSerializer(user),
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})

我认为您应该参考以下解决方案:

哪个州

默认情况下,$http服务将通过以下方式转换传出请求: 将数据序列化为JSON,然后将其与内容一起发布- 键入“application/json”。当我们想将值作为表单发布时 post,我们需要更改序列化算法并发布数据 内容类型为“application/x-www-form-urlencoded”

所以你应该这样做:

$http({
    method: 'POST',
    url: url,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    transformRequest: function(obj) {
        var str = [];
        for(var p in obj)
        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        return str.join("&");
    },
    data: {username: $scope.userName, password: $scope.password}
}).success(function () {});

我不明白问题出在哪里。你的意思是你的web api没有收到请求吗?它的格式不正确。看看帖子的数据,我想要的是用户名和密码。应用程序正在使用
{“密码”:“d”,“电子邮件”:d@s“}
as key in whisto只是说$_POST[“email”]为空,但值在$_POST[“{”password:“d”,“email:”中”d@s“}']是的,我上次试过了,显然需要等一段时间,现在就可以:)虽然我很确定这个答案是正确的,但我选择了上面的一个,因为它更高效,而且我需要编写更少的代码。但不管怎样,我都会给你一票:D
$http({
    method: 'POST',
    url: url,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    transformRequest: function(obj) {
        var str = [];
        for(var p in obj)
        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        return str.join("&");
    },
    data: {username: $scope.userName, password: $scope.password}
}).success(function () {});