简体   繁体   English

AngularJS无法将POST请求发送到SLIM Framework

[英]AngularJS can't sent POST request to SLIM Framework

Hello I have this angularjs http.post() request to slim php framework. 您好,我有这个angularjs http.post()请求,用于苗条的php框架。

  'use strict';

   soup.factory('loginService',function($http){
    return {
        login:function(user){
                         console.log(user);

            var $promise = $http.post('api/vendor/slim/slim',user );
            return $promise;
    };
});

when I debug the $_POST it always return an empty array, 当我调试$_POST它总是返回一个空数组,

but when I use use json_decode(file_get_contents('php://input')) , slim php will return an error. 但是当我使用json_decode(file_get_contents('php://input')) ,苗条的php将返回错误。

this is php function that handle the request. 这是处理请求的php函数。

function login(){
    $sql = "SELECT * FROM users ORDER by id";
     try {
        $db = getConnection();
        $stmt = $db->query($sql);
        $users = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        //print_r($users);
        $users = json_encode($users);


    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }

    var_dump($_POST);
    $test = json_decode(file_get_contents('php://input'));

    //do something with the post data



}

how can I solve this? 我该如何解决?

$http api say: $ http api说:

params – {Object.} – Map of strings or objects which will be turned to ?key1=value1&key2=value2 after the url. params – {Object。} –字符串或对象的映射,将在URL后变成?key1 = value1&key2 = value2。 If the value is not a string, it will be JSONified. 如果该值不是字符串,则将被JSON化。

Try sending data as a string, something like this: 尝试以字符串形式发送数据,如下所示:

var $promise = $http({
    url: 'api/vendor/slim/slim',                                                method: 'POST',
    data: 'user=' + usr + '&password=' + pwd,
    headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
});

Don't forget to include the Content-Type header. 不要忘记包含Content-Type标头。

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

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