2

I'm trying to send a post form to a PHP script, and every time, the POST request shows as canceled.

enter image description here

My controller looks like this:

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

authenticationControllers.controller('Authentication', ['$scope', '$http', function($scope, $http) {
    $scope.WPlogin = function () {
    var mydata ={ 
                  'action': 'ajaxlogin', 
                  'username': $scope.user.username, 
                  'password': $scope.user.password, 
                  'security-login': pluginParams.nonce,
                  '_wp_http_referer': '/s/play/'
                };

    $http({
      method: 'POST',
      url: $scope.ajaxURL, //This is defined earlier, I'm sure this is valid
      data: jQuery.param(mydata),
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}
      }).success(function(data, status, headers, config) {
        console.log(data.message);
      }).error(function(e){alert('error')});

  };
}]);

And the form looks like this:

   <div class="white-popup mfp-hide" id="login-modal" ng-controller="Authentication">
    <form name="loginform" id="loginform" novalidate>
        <h1>Login</h1>
        <p class="status"></p>
        <label for="username">Username</label>
        <input id="username" type="text" name="username" ng-model="user.username" ng-required="true">
        <label for="password">Password</label>
        <input id="password" type="password" name="password" ng-model="user.password" ng-required="true">
        <a class="lost" href="<?php echo wp_lostpassword_url(); ?>">Lost your password?</a>
        <button class="submit_button" ng-disabled="loginform.$invalid" ng-click="WPlogin()" type="submit" name="submit"> Login</button>    

    </form>
</div>

Whenever I try to submit the form, the error alert pops up, but no console errors. Then, the page reloads, and I can see the form parameters in the URL (like a get request), and if I then submit my form (without deleting the get parameters), then the request is a success.

Can anyone tell me what I'm doing wrong?

Update

I added $event.preventDefault(); to the form (passing $event from the form), and now it all works as expected, but I don't understand, why do I need that? I thought AngularJS would automatically prevent the form submission.

coopersita
  • 5,011
  • 3
  • 27
  • 48
  • 1. Your code at plunker: http://plnkr.co/edit/3GBvIr7ZTja8cMmantIU?p=preview Where I removed `pluginParams.nonce` and `jQuery.param(mydata)`. It does post... 2. Why u use jQuery methods? 3. Check http-interceptors if u have any. – Petr Averyanov Nov 28 '14 at 18:39
  • There most be something wrong with my browser, because your plunker also triggers the error alert. I used the jQuery to send the script a regular query string, instead of JSON: http://stackoverflow.com/questions/11442632/how-can-i-post-data-as-form-data-instead-of-a-request-payload – coopersita Nov 28 '14 at 21:15

0 Answers0