2015-11-23 66 views
0

我想要从serice页面到php页面的多个值。我想将'data','albimg','albvideo'传递给php page.now我发现错误是如图所示。 enter image description here从js页面传递多个值到php页面

var deferred = $q.defer(); 
      data.pagename = "create_album"; 
      $http.post('js/data/album.php', {action:{"data":data,"albimg":albimg,"albvideo":albvideo}}) 
        .success(function (data, status, headers, config) 
        { 
         console.log(status + ' - ' + action); 
         deferred.resolve(action); 
        }) 
        .error(function (action, status, headers, config) 
        { 
         deferred.reject(action); 
         console.log('error'); 
        }); 

      return deferred.promise; 
php page: 
$postdata = file_get_contents("php://input",true); 
$request = json_decode($postdata); 
$now = date('Y-m-d H:i:s'); 
echo $sql="INSERT INTO `$prefix.album` (CONTENT_VALUES,CreatedTime)VALUES('$postdata','$now')"; 
+1

行动。上面的代码,行动不是一个变量,(是一个属性) – Ray

回答

1

您可以通过使用PARAM属性做到这一点:像这样;

var data = {"data":data,"albimg":albimg,"albvideo":albvideo}; 

$http({ url: "js/data/album.php", method: "POST", params: data }) 
1

看看这个例子:

function PhoneListCtrl($scope, phones) { 
     $scope.phones = phones; 
     $scope.orderProp = 'age'; 
    } 

    PhoneListCtrl.resolve = { 
     phones: function(Phone, $q) { 
     var deferred = $q.defer(); 



deferred.reject(); 
    Phone.query(function(successData) { 
      deferred.resolve(successData); 
    }, function(errorData) { 
      deferred.reject(); 
    }); 
    return deferred.promise; 
    }, 
    delay: function($q, $defer) { 
    var delay = $q.defer(); 
    $defer(delay.resolve, 1000); 
    return delay.promise; 
    } 
} 

我想你忘记添加$defer给你的函数。你真的需要异步吗?如果您使用$q这是您需要它的Beacouse。但如果你只是想发送数据到php文件最简单的方式来使用这样的事情:

angular.module('httpExample', []) 
.controller('FetchController', ['$scope', '$http', '$templateCache', 
    function($scope, $http, $templateCache) { 
    $scope.method = 'Post'; 
    $scope.url = 'http-hello.php'; 

    $scope.fetch = function() { 
     $scope.code = null; 
     $scope.response = null; 

     $http({method: $scope.method, url: $scope.url, cache: $templateCache}). 
     then(function(response) { 
      $scope.status = response.status; 
      $scope.data = response.data; 
     }, function(response) { 
      $scope.data = response.data || "Request failed"; 
      $scope.status = response.status; 
     }); 
    }; 

    $scope.updateModel = function(method, url) { 
     $scope.method = method; 
     $scope.url = url; 
    }; 
    }]); 
1

成功回调没有定义动作参数。

您的代码

.success(function (data, status, headers, config) // No Action here 
    { 
     console.log(status + ' - ' + action); // This line gives error 
     deferred.resolve(action); 
    }) 

应该是没有定义

.success(function (data, status, headers, config, action) // Add Action here 
     { 
      console.log(status + ' - ' + action); 
      deferred.resolve(action); 
     })