Skip to main content
Copy edited (e.g. ref. <http://en.wikipedia.org/wiki/JavaScript>, <http://en.wikipedia.org/wiki/Ajax_%28programming%29>, and <http://en.wikipedia.org/wiki/PHP>). Removed meta information (this belongs in comments).
Source Link

Can't get a JSON object in response to an ajaxAjax request with wp_ajax

I have a problem with WordPress and ajaxAjax.

This is my javascriptJavaScript part (I trimmed it a bit):

The script gets the ajaxAjax response from admin-ajax. Unfortunately the console throws an error when it gets to the each statement in javascriptthe JavaScript code... it says:

"Uncaught TypeError: Cannot use 'in' operator to search for '4' in Array". 

If I do a console.log of my "posts" var I get a string 'Array'. No matter how I pass the $list variable in PHP it will always return a string. The query returns posts elsewhere, so it's not empty. I tried without json_encode, with and without declaring header, using wp_send_json(), putting ob_clean() before echoing the array, putting the array into an array... But it always gets into ajaxajax as a string Array and each cannot cycle through it.

This should be a very simple thing and I can't understand why it's not working. I don't have other javascriptJavaScript or phpPHP errors or warnings and everything else runs fine. I'm calling it a day and getting to sleep while posting this on stack :)

Can't get a JSON object in response to an ajax request with wp_ajax

I have a problem with WordPress and ajax.

This is my javascript part (I trimmed it a bit):

The script gets the ajax response from admin-ajax. Unfortunately the console throws an error when it gets to the each statement in javascript... it says:

"Uncaught TypeError: Cannot use 'in' operator to search for '4' in Array". 

If I do a console.log of my "posts" var I get a string 'Array'. No matter how I pass the $list variable in PHP it will always return a string. The query returns posts elsewhere, so it's not empty. I tried without json_encode, with and without declaring header, using wp_send_json(), putting ob_clean() before echoing the array, putting the array into an array... But it always gets into ajax as a string Array and each cannot cycle through it.

This should be a very simple thing and I can't understand why it's not working. I don't have other javascript or php errors or warnings and everything else runs fine. I'm calling it a day and getting to sleep while posting this on stack :)

Can't get a JSON object in response to an Ajax request with wp_ajax

I have a problem with WordPress and Ajax.

This is my JavaScript part (I trimmed it a bit):

The script gets the Ajax response from admin-ajax. Unfortunately the console throws an error when it gets to the each statement in the JavaScript code... it says:

"Uncaught TypeError: Cannot use 'in' operator to search for '4' in Array".

If I do a console.log of my "posts" var I get a string 'Array'. No matter how I pass the $list variable in PHP it will always return a string. The query returns posts elsewhere, so it's not empty. I tried without json_encode, with and without declaring header, using wp_send_json(), putting ob_clean() before echoing the array, putting the array into an array... But it always gets into ajax as a string Array and each cannot cycle through it.

This should be a very simple thing and I can't understand why it's not working. I don't have other JavaScript or PHP errors or warnings and everything else runs fine.

Tweeted twitter.com/#!/StackWordPress/status/537001274009747456
added 32 characters in body
Source Link
unfulvio
  • 1.8k
  • 7
  • 32
  • 63

I have a problem with WordPress and ajax.

This is my javascript part (I trimmed it a bit):

var posts = $.ajax({
    type: 'POST',
    url: ajaxurl,
    async: false,
    dataType: 'json',
    data: { action: 'myAjaxFunc' },
    done: function(response) {
        return response;
    }
}).responseText;

$.each(posts, function() {
    $('#someSelect').append( $('<option</option>').text(this.name).val(this.id) );
});

My PHP code is as follows:

function myAjaxFunc() {

    $posts = get_posts( array(
        'posts_per_page'   => -1,
        'orderby'          => 'title',
        'order'            => 'ASC',
        'post_type'        => 'my-post-type',
        'post_status'      => array( 'publish', 'draft' )
    ) );

    $list = array();
    foreach ( $posts as $post ) {
        $list[] = array(
            'id'   => $post->ID,
            'name' => $post->post_title,
            'link' => get_permalink( $post->ID ),
        );
    }

    header("Content-type: application/json");
    echo json_encode( $list );
    die;
}
add_action( 'wp_ajax_nopriv_myAjaxFunc', 'myAjaxFunc' );
add_action( 'wp_ajax_myAjaxFunc', 'myAjaxFunc' );

The script gets the ajax response from admin-ajax. Unfortunately the console throws an error when it gets to the each statement in javascript... it says:

"Uncaught TypeError: Cannot use 'in' operator to search for '4' in Array". 

If I do a console.log of my "posts" var I get a string 'Array'. No matter how I pass the $list variable in PHP it will always return a string. The query returns posts elsewhere, so it's not empty. I tried without json_encode, with and without declaring header, using wp_send_json(), putting ob_clean() before echoing the array, putting the array into an array... But it always gets into ajax as a string Array and each cannot cycle through it.

This should be a very simple thing and I can't understand why it's not working. I don't have other javascript or php errors or warnings and everything else runs fine. I'm calling it a day and getting to sleep while posting this on stack :)

I have a problem with WordPress and ajax.

This is my javascript part (I trimmed it a bit):

var posts = $.ajax({
    type: 'POST',
    url: ajaxurl,
    async: false,
    dataType: 'json',
    data: { action: 'myAjaxFunc' },
    done: function(response) {
        return response;
    }
}).responseText;

$.each(posts, function() {
    $('#someSelect').append( $('<option</option>').text(this.name).val(this.id) );
});

My PHP code is as follows:

function myAjaxFunc() {

    $posts = get_posts( array(
        'posts_per_page'   => -1,
        'orderby'          => 'title',
        'order'            => 'ASC',
        'post_type'        => 'my-post-type',
        'post_status'      => array( 'publish', 'draft' )
    ) );

    $list = array();
    foreach ( $posts as $post ) {
        $list[] = array(
            'id'   => $post->ID,
            'name' => $post->post_title,
            'link' => get_permalink( $post->ID ),
        );
    }

    header("Content-type: application/json");
    echo json_encode( $list );
    die;
}
add_action( 'wp_ajax_nopriv_myAjaxFunc', 'myAjaxFunc' );
add_action( 'wp_ajax_myAjaxFunc', 'myAjaxFunc' );

The script gets the ajax response from admin-ajax. Unfortunately the console throws an error when it gets to the each statement in javascript... it says:

"Uncaught TypeError: Cannot use 'in' operator to search for '4' in Array". 

If I do a console.log of my "posts" var I get a string 'Array'. No matter how I pass the $list variable in PHP it will always return a string. The query returns posts elsewhere, so it's not empty. I tried without json_encode, with and without declaring header, putting ob_clean() before echoing the array, putting the array into an array... But it always gets into ajax as a string and each cannot cycle through it.

This should be a very simple thing and I can't understand why it's not working. I don't have other javascript or php errors or warnings and everything else runs fine. I'm calling it a day and getting to sleep while posting this on stack :)

I have a problem with WordPress and ajax.

This is my javascript part (I trimmed it a bit):

var posts = $.ajax({
    type: 'POST',
    url: ajaxurl,
    async: false,
    dataType: 'json',
    data: { action: 'myAjaxFunc' },
    done: function(response) {
        return response;
    }
}).responseText;

$.each(posts, function() {
    $('#someSelect').append( $('<option</option>').text(this.name).val(this.id) );
});

My PHP code is as follows:

function myAjaxFunc() {

    $posts = get_posts( array(
        'posts_per_page'   => -1,
        'orderby'          => 'title',
        'order'            => 'ASC',
        'post_type'        => 'my-post-type',
        'post_status'      => array( 'publish', 'draft' )
    ) );

    $list = array();
    foreach ( $posts as $post ) {
        $list[] = array(
            'id'   => $post->ID,
            'name' => $post->post_title,
            'link' => get_permalink( $post->ID ),
        );
    }

    header("Content-type: application/json");
    echo json_encode( $list );
    die;
}
add_action( 'wp_ajax_nopriv_myAjaxFunc', 'myAjaxFunc' );
add_action( 'wp_ajax_myAjaxFunc', 'myAjaxFunc' );

The script gets the ajax response from admin-ajax. Unfortunately the console throws an error when it gets to the each statement in javascript... it says:

"Uncaught TypeError: Cannot use 'in' operator to search for '4' in Array". 

If I do a console.log of my "posts" var I get a string 'Array'. No matter how I pass the $list variable in PHP it will always return a string. The query returns posts elsewhere, so it's not empty. I tried without json_encode, with and without declaring header, using wp_send_json(), putting ob_clean() before echoing the array, putting the array into an array... But it always gets into ajax as a string Array and each cannot cycle through it.

This should be a very simple thing and I can't understand why it's not working. I don't have other javascript or php errors or warnings and everything else runs fine. I'm calling it a day and getting to sleep while posting this on stack :)

Source Link
unfulvio
  • 1.8k
  • 7
  • 32
  • 63

Can't get a JSON object in response to an ajax request with wp_ajax

I have a problem with WordPress and ajax.

This is my javascript part (I trimmed it a bit):

var posts = $.ajax({
    type: 'POST',
    url: ajaxurl,
    async: false,
    dataType: 'json',
    data: { action: 'myAjaxFunc' },
    done: function(response) {
        return response;
    }
}).responseText;

$.each(posts, function() {
    $('#someSelect').append( $('<option</option>').text(this.name).val(this.id) );
});

My PHP code is as follows:

function myAjaxFunc() {

    $posts = get_posts( array(
        'posts_per_page'   => -1,
        'orderby'          => 'title',
        'order'            => 'ASC',
        'post_type'        => 'my-post-type',
        'post_status'      => array( 'publish', 'draft' )
    ) );

    $list = array();
    foreach ( $posts as $post ) {
        $list[] = array(
            'id'   => $post->ID,
            'name' => $post->post_title,
            'link' => get_permalink( $post->ID ),
        );
    }

    header("Content-type: application/json");
    echo json_encode( $list );
    die;
}
add_action( 'wp_ajax_nopriv_myAjaxFunc', 'myAjaxFunc' );
add_action( 'wp_ajax_myAjaxFunc', 'myAjaxFunc' );

The script gets the ajax response from admin-ajax. Unfortunately the console throws an error when it gets to the each statement in javascript... it says:

"Uncaught TypeError: Cannot use 'in' operator to search for '4' in Array". 

If I do a console.log of my "posts" var I get a string 'Array'. No matter how I pass the $list variable in PHP it will always return a string. The query returns posts elsewhere, so it's not empty. I tried without json_encode, with and without declaring header, putting ob_clean() before echoing the array, putting the array into an array... But it always gets into ajax as a string and each cannot cycle through it.

This should be a very simple thing and I can't understand why it's not working. I don't have other javascript or php errors or warnings and everything else runs fine. I'm calling it a day and getting to sleep while posting this on stack :)