dtmm0148603 2014-10-17 13:21
浏览 42
已采纳

在$ http.post传递的PhP脚本中使用变量

In angular im using $http.post for sending an id to a php script in order to use this id for a mysql request.

This is my controller:

function ProjectDetailsCtrl($scope, $http, $timeout, getGoodIdProjectDetails) {

    $scope.idProjectDetails = getGoodIdProjectDetails.getId(); //Getting id project

    $scope.$emit('LOAD')
    $scope.url = 'scripts/findProjectDetails.php';

    $http.post($scope.url, { "idProject" : $scope.idProjectDetails}).
    success(function(data, status) {
        $scope.projectDetails = {};
        $scope.projectDetails.details = data;
        $scope.$emit('UNLOAD')
    })
    .
    error(function(data, status) {
        $scope.data = data || "Request failed";
        $scope.status = status;         
    });
}

idProjectDetails is a number.

Then the Php script :

<?php
$idProject = json_decode($_POST['idProject']);

Php script returns that $idProject is undefined.

Can you help me with this ?

Edit : I tried this way but my app crashed with this :

$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
$http({
    method: 'POST',
    url: 'scripts/findProjectDetails.php',
    data: "idProject" : $scope.idProjectDetails  
}).success(function(data, status) {
    $scope.projectDetails = {};
    $scope.projectDetails.details = data;
    console.log(projectDetails.details);
    $scope.$emit('UNLOAD')
})
.
error(function(data, status) {
    $scope.data = data || "Request failed";
    $scope.status = status;         
});

With the first example, in Chrome console, the "request playload" contain this : {idProject:1} idProject:1 so I assume my variable is correctly passed through the php script?

  • 写回答

1条回答 默认 最新

  • dsbifvcxr458755493 2014-10-17 13:25
    关注

    There is much confusion among newcomers to AngularJS as to why the $http service shorthand functions ($http.post(), etc.) don’t appear to be swappable with the jQuery equivalents (jQuery.post(), etc.) The difference is in how jQuery and AngularJS serialize and transmit the data. Fundamentally, the problem lies with your server language of choice being unable to understand AngularJS’s transmission natively ... By default, jQuery transmits data using Content-Type: x-www-form-urlencoded and the familiar foo=bar&baz=moe serialization. AngularJS, however, transmits data using Content-Type: application/json and { "foo": "bar", "baz": "moe" } JSON serialization, which unfortunately some Web server languages—notably PHP—do not unserialize natively.

    So concretely, your $_POST variable is empty !

    To go through this problem there're 2 solutions:

    I haven't invented anything here, just linking... Hope it'll help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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,如何解決?
  • ¥15 c++头文件不能识别CDialog