简体   繁体   English

在POST表单上的Yeoman,Grunt,AngularJS和错误404

[英]Yeoman, Grunt, AngularJS and error 404 on POST form

I am trying to send a form with "POST" method with AngularJS. 我正在尝试使用AngularJS发送带有“POST”方法的表单。 I use $http to send it but it always return "Cannot POST" and 404 error. 我使用$ http发送它,但它总是返回“无法POST”和404错误。 The route is correct and only happens if I execute my app via Grunt (sending by "GET" works perfect). 路线是正确的,只有当我通过Grunt执行我的应用程序时才会发生(通过“GET”发送完美)。 The app has been built using Yeoman. 该应用程序是使用Yeoman构建的。

I think that it has to be a silly problem but I cannot make it work. 我认为它必须是一个愚蠢的问题,但我无法使其发挥作用。

The view: 风景:

<form name="formLogin" ng-submit="login()">

<ul>
    <li>
        <label for="user">User</label>
        <input type="text" id="user" name="user" ng-model="user">
    </li>
    <li>
        <label for="pass">Password</label>
        <input type="password" id="pass" name="pass" ng-model="pass">
    </li>

</ul>

<input type="submit" value="Send">

The controller: 控制器:

    'use strict';
angular.module('myApp')
  .controller('LoginCtrl', function ($scope, $http, $resource) {


    $scope.login = function() {

      var config = {
        url: 'php/login.php',
        method: 'POST',
        data: {
            user: $scope.user,
            pass: $scope.pass
        }
      }; // configuration object

      $http(config)
        .success(function(data, status, headers, config) {
            console.log('success');
            if (data.status) {
              // succefull login

            } else {
              // Wrong login
            }
        })
        .error(function(data, status, headers, config) {
            console.log('error');

        });

    };

  });

I use Grunt with: 我用Grunt:

grunt server

Why happens that? 为什么会这样?

Thanks! 谢谢!

I'm confused why GET should work in this case. 我很困惑为什么GET应该在这种情况下工作。 The Grunt server just serves static files from your app and .tmp directories and your reference to the .php file is relative. Grunt服务器只提供来自app.tmp目录的静态文件,并且对.php文件的引用是相对的。 So with the standard setup, an AJAX request to php/login.php would just return you the plain PHP source code of the file located in app/php/login.php , which is certainly not what you want. 因此,对于标准设置,对php/login.php的AJAX请求只会返回位于app/php/login.php中的文件的纯PHP源代码,这当然不是您想要的。

You probably want to separate your backend from your frontend and have your PHP application in a different location. 您可能希望将后端与前端分开,并将PHP应用程序放在其他位置。 This could either be under a different location (eg /api/login.php ) or a different host https://api.example.com/login.php . 这可能位于不同的位置(例如/api/login.php )或不同的主机https://api.example.com/login.php Mixing your Yeoman application with your API isn't a good idea. 将Yeoman应用程序与API混合使用并不是一个好主意。

However, if you are forced to keep them within one project, you can use grunt-php to start a local PHP server from your Gruntfile. 但是,如果您被迫将它们保存在一个项目中,则可以使用grunt-php从Gruntfile启动本地PHP服务器。

Angular by default sends http post requests as application/json and not as application/x-www-form-urlencoded which may cause problems. Angular默认发送http post请求作为application/json而不是application/x-www-form-urlencoded ,这可能会导致问题。 Change your application to accept application/json or replace application/json with application/x-www-form-urlencoded in $httpProvider.defaults.headers.post and serialize form yourself. 更改您的应用程序以接受application/json或将application/json $httpProvider.defaults.headers.post$httpProvider.defaults.headers.post application/x-www-form-urlencoded$httpProvider.defaults.headers.post序列化。 More details in Angular.js doc in section Setting HTTP headers. 更多详细信息,请参阅设置HTTP标头部分中的Angular.js文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM