dongshen2903 2014-09-24 12:42
浏览 80

AngularJS $ http发布到PHP页面

Finally got this working a $http POST after days of trying to find an answer. Would like to know why I have had to use two different sets of PHP code for two AngularJS $http POST requests. This are my AngularJS $http POST requests:

ContactsControllers.factory('messageFactory', ['$http', function($http){
    return {
        sendMessage: function(message,contactIdArray){
            return $http.post('/php/sendMessage.php', {message:message,contactIdArray:contactIdArray});
            }
        };
}]);

ContactsControllers.factory('messagesFactory', ['$http', function($http){
    return {
        getContactMessages: function(contactid){
            var config = {
                params: {
                    contactid: contactid
                }   
            };
            return $http.post('/php/getMessages.php',null,config);
        }
    };
}]);

and my two PHP code sets:

$data = file_get_contents("php://input");
$data = json_decode($data, TRUE);
$thisMessage = $data['message'];
$contactIdArray = $data['contactIdArray'];
if($_SERVER["REQUEST_METHOD"] === "POST")
{
    if(isset($_GET["contactid"]))
    {
        $contactid = json_decode($_GET["contactid"]);
    }
    else{
        $result = "no contact id";
    }
    echo json_encode($contactid);
}

If someone could tell me why I could not just use the first sets of angularJS and PHP code for my 'messagesFactory' $http POST request, it will help me understand this a bit better. Like I said after a couple of days of trying out different code I found this http://www.angularjshub.com/examples/forms/formsubmission/ which I finally got working.

</div>
  • 写回答

1条回答 默认 最新

  • 普通网友 2015-08-27 12:17
    关注

    file_get_contents("php://input") only gives access to POST data. So you can't use it to get access to query string data (GET) and incidentally it doesn't allow access to POST data if you've included enctype="multipart/form-data" in your form either.

    The reason your first php script works is because the data you are asking for is POST variables.

    The reason your first php script does not work for your second request where the data is in the url is that file_get_contents("php://input") can't access GET data.

    The solutions you could use are to implement a true GET request instead of POST from $http, to send all data as POST not GET using $http, or to do as you have and catch the data using a different method in your php script.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?