dongna9185 2015-09-03 05:32
浏览 56
已采纳

通过Angular JS $ http.post使用slim框架时,404无效的http状态代码

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条回答 默认 最新

  • dua55014 2015-09-03 15:32
    关注

    As per my comment:

    There is an undefined var $data

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?