2015-11-23 2 views
0

Я хочу несколько значений от страницы serice до php. Я хочу передать «данные», «albimg», «albvideo» на страницу php.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

вы можете сделать это с помощью паров свойства: как это;

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 к вашей функции. Вы действительно нуждаетесь асинхронно? Beacouse, если вы используете $q, это то, что вам нужно. Но если вы просто хотите, чтобы отправить данные в 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); 
     }) 
Смежные вопросы