0

I'm building a webapp with yeoman tools using AngularJS, and am having trouble sending a POST request to a server side script from a controller. Here is the function in my controller code:

$scope.addUser = function() {
  $http.post('addUser.php', $scope.user).success(function(response) {
    console.log('success');
    console.log(response);
    $scope.showForm = false;
    $scope.infoSubmitted = true;
  }).error(function(err){
    console.log('failure');
    console.log(err);
  });

};

The addUser.php file is located in the app/ directory, along with index.html. I am a little confused as to how AngularJS handles routing, as well. I know that I can define routes for GET requests in the app.js file with .when({ ... }), but I can't see how to handle POST requests and call server side scripts.

This is the .php file I want to connect to:

<?php

$errors     = array();    // array to hold validation errors
$data       = array();    // array to pass back data

// validate the variables ======================================================
if (empty($_POST['name']))
$errors['name'] = 'Name is required.';

if (empty($_POST['email']))
$errors['email'] = 'Email is required.';

// return a response ===========================================================

// response if there are errors
if ( ! empty($errors)) {

// if there are items in our errors array, return those errors
  $data['success'] = false;
  $data['errors']  = $errors;
} else {

// if there are no errors, return a message
  $data['success'] = true;
  $data['message'] = 'Success!';
}

// return all our data to an AJAX call
echo json_encode($data);
5
  • I'd suggest you added full information about the error (from Firebug or equivalent); then maybe even the php file. Commented Apr 25, 2014 at 19:36
  • try throwing a "/" before the addUser.php. Are you seeing the post request in dev tools? Commented Apr 25, 2014 at 19:39
  • This is the error in Dev tools: POST http://127.0.0.1:9000/addUser.php 404 (Not Found). This was the same before I added a "/" before "addUser.php" Commented Apr 25, 2014 at 19:46
  • can you use a tool like postman and see if you can post a request to addUser.php? That should help determine if the problem is client or server side. Commented Apr 25, 2014 at 20:04
  • I ended up using the generator-angular-fullstack which provides a file for inputting routing information. Commented Apr 27, 2014 at 19:37

1 Answer 1

1

Try to put addUser.php to the subfolder - /app/server/addUser.php and do
$http.post('server/addUser.php', $scope.user)...
Also php files will not work with default generator-angular. You need generator-angular-php.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.