weixin_33720078 2015-06-08 22:24 采纳率: 0%
浏览 33

带有AngularJS的Wordpress AJAX

I'm trying to perform an ajax request in wordpress using angularjs. After a while I managed to get it working. However, I'm not sure this is the best solution. I'm also not able to pass objects as parameters.

$scope.submitForm = function() {
        $http({
            method: 'POST',
            url: APP.ajaxurl,
            params: {
                action: "form-submission",
                car: $scope.car.selectedOptions.name,
                extras: $scope.car.selectedOptions.extras,
            },
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            console.log(data);
        });
    }

This works, but in my functions.php I have to use $_REQUEST instead of $_POST and if I test the code below I get an error because extras is an object.

add_action( 'wp_ajax_nopriv_form-submission', 'submit_form' );
add_action( 'wp_ajax_form-submission', 'submit_form' );

function submit_form(){
    echo $_REQUEST['extras'];
    wp_die();
}

Any ideas?

  • 写回答

1条回答 默认 最新

  • 普通网友 2017-02-01 13:00
    关注

    In Angular

               params: {
                action: "form-submission",
                car: $scope.car.selectedOptions.name,
                extras: JSON.stringify($scope.car.selectedOptions.extras),
            }
    

    In PHP

    function submit_form(){
    echo json_decode($_REQUEST['extras']);
    wp_die();
    

    }

    评论

报告相同问题?