0

I did alot of research on my problem and none the solution from the internet help. Hope you guys can help me.

My browser console shown the following error when I clicked on the submit button:

enter image description here

My Rest api code using Slim Framework:

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization");

require 'vendor/autoload.php';
require 'vendor/notorm/notORM.php';

$app = new \Slim\Slim();

$db_username = 'postgres';
$db_password = 'postgres';
$dsn = 'pgsql:host=localhost;dbname=platapp';
$conn = new PDO($dsn, $db_username, $db_password) or die ("can't connect");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db = new NotORM($conn) or die ("instance not created");


$app->post('/registration', function() use($app, $db, $conn){
   $app->response()->header("Content-Type", "application/json");
   $post = json_decode($app->request()->getBody());
   $postArray = get_object_vars($post);
   $user = $db->user_platapp();
   $result = $user->insert($data);

   $db = null;
   $conn = null;
});

My angular code:

app.controller('regProfileCtrl', function($http, $state, $scope,$stateParams, registration){

$scope.phone = $stateParams.phone;
$scope.setPage = function(page){
        $state.go(page);
};
$scope.setUser = function(user, page){
    var url = "http://localhost:3000/www/api/index.php/registration";
    var data = {
        user_given_name: user.name, 
        user_email: user.email, 
        user_password : user.password, 
        user_phone: $scope.phone, 
        user_last_name: "Malaysia", 
        user_country: "677"
    };


    var error_string = "Password not matched.";
    if(user.password == user.confirmpassword){
        $http.post(url, data).then(function(response){
                            $scope.setPage(page);
                            });
    }else
    {
        alert(error_string);
    }
}
}); 

Fyi, using app->get(...) got no problem.. only at POST got problem..

2 Answers2

1

As per my comment:

There is an undefined var $data

$result = $user->insert($data);

danopz
  • 3,310
  • 5
  • 31
  • 42
0

It looks like you got the header error. There is a stackoverflow post Getting 404 error on POST. Angular.js that might be similar with your problem.

To summarize it, you can modify the header in your code such as stated below.

Latest version of Chrome does not need this header

header('Access-Control-Allow-Origin: *');

Extra header is not necessary. You have to use this header instead

header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

You may refer to this StackOverflow post Error :Request header field Content-Type is not allowed by Access-Control-Allow-Headers which explained it in details.

Community
  • 1
  • 1
Alif Jamaluddin
  • 518
  • 4
  • 17