Antoine Dionne
Antoine Dionne

Reputation: 121

Ajax Works in Firefox but not in Chrome

Everything works well in firefox, all the data return a value, but on chrome, they are all undefined. I tried to empty my function with only a simple string return ex. 'asd' and this still return Undefined. Working on wordpress.

$('.vote-synergy-up, .vote-synergy-down').click(function(){
    var data_synergy = $(this).parent('.vote-synergy-container').attr('data-synergy'); /*PK SYNERGY*/
    var VoteType = '';
    var OtherVoteType = '';

    if ($(this).hasClass('vote-synergy-up')){
        VoteType = 'Up';
        OtherVoteType = 'Down';
    }

    if ($(this).hasClass('vote-synergy-down')){
        VoteType = 'Down';
        OtherVoteType = 'Up';
    }

    // This does the ajax request
    $.ajax({
        type: "POST",
        url: omvp_ajax.ajax_url,
        dataType: 'json',
        data: {
            'action':'synergy_vote',
            'pk_Synergy' : data_synergy,
            'VoteType' : VoteType
        },
        success:function(data) {
            alert(data.Test)
        },
        error: function(errorThrown){

            alert('An error as occured');
            console.log(errorThrown);
        }
    });  
});

Here is my function

function synergy_vote() {

if ( isset($_REQUEST) ) {
    echo json_encode(
        array(
            'Test'=>'ASD'
            )
    );
}

}

Upvotes: 1

Views: 670

Answers (2)

Antoine Dionne
Antoine Dionne

Reputation: 121

Replaced

add_action( 'wp_ajax_synergy_vote', 'synergy_vote' ); // It called from the back end

With

add_action( 'wp_ajax_nopriv_synergy_vote', 'synergy_vote' );    // If called from front end

Upvotes: 1

TheCarver
TheCarver

Reputation: 19713

Try parsing the JSON that's returned from the server.

success:function(data) {
    var d = $.parseJSON(data);
    alert(d.Test);
},

Upvotes: 0

Related Questions