1

I am sending array to PHP through angularJs $http, but when I receiving data it was showing null, I don't know is that my procedure is correct or not,

js file is

var newdisable_comments_on_post_types = JSON.stringify(allTabData.disable_comments_on_post_types);
$http({
        method:'post',
        url:url,
        params:{
       'disable_comments_on_post_types': newdisable_comments_on_post_types
                }
        });

while sending in the header it sending like this

disable_comments_on_post_types:{"post":false,"page":false,"attachment":false}

in the PHP file, i did some of the procedure to receive it

$a = $_POST['disable_comments_on_post_types']['post'];// method 1

$a = $_POST['disable_comments_on_post_types'] // method 2
$x=1
foreach($a as $val){
$b[$x]=$val;
$x++;
}

$a = $_POST['disable_comments_on_post_types']->post;// method 3

I am getting null in response every method while I returning data to check

echo json_encode($a);

am I doing any wrong or in WordPress we cant send an array to PHP?

1 Answer 1

1

Change Your $http service to this:

By default, the $http service will transform the outgoing request by serializing the data as JSON and then posting it with the content- type, "application/json"

    $http({
        method: 'POST',
        url: url,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        transformRequest: function(obj) {
            var str = [];
            for(var p in obj)
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
            return str.join("&");
        },
        data: allTabData.disable_comments_on_post_types
    }).success(function () {});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.