【问题标题】:PHP not receiving post from $resource AngularJSPHP没有收到来自$resource AngularJS的帖子
【发布时间】:2014-07-10 05:57:25
【问题描述】:

我在 Angular 中有一个工厂服务,它成功地将我的电子邮件发布到我已使用 Firebug 验证的 HTTP 请求中,但我的 PHP 文件没有看到该电子邮件。我正在访问我的 PHP 文件,因为我可以回显它的响应,但是我发送它的电子邮件值没有被收到,正如我已经提到的那样。

控制器(部分):

$scope.subscribe = function() {
        if($scope.email.length > 0) {
            Subscribe.save({},$scope.email);
        } else {
            alert("Please enter your email address");
        }
    }

工厂:

app.factory('Subscribe', ['$resource', function($resource) {
    return $resource( 'server/restdb.php:email', 
        {email: '@email'},
        {

        }
    ); 
}]);

PHP:

<?php
echo 'email: '.$_REQUEST['email'];

我也尝试使用$_POST 来获取电子邮件。

回顾一下:

  • 在 HTTP 请求中成功发布电子邮件。
  • 正在访问 PHP 文件并从中获取响应。
  • PHP 没有收到我发送的电子邮件值。

【问题讨论】:

    标签: php angularjs rest ngresource


    【解决方案1】:

    哦,$resource.save 有点“奇怪”。

    你应该这样使用它:

    var myData = new Subscribe({email: $scope.email});
    myData.save();
    

    您可以将工厂简化为:

    app.factory('Subscribe', ['$resource', function($resource) {
        return $resource( 'server/restdb.php', {}, {}
        ); 
    }]);
    

    多看一下文档:angular.js $resources

    【讨论】:

      【解决方案2】:

      尝试使用这个:

      $aRequest = json_decode(file_get_contents("php://input"), true);
      echo 'email: ' . $aRequest['email'];
      

      【讨论】:

      猜你喜欢
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      相关资源
      最近更新 更多