【问题标题】:Ajax Works in Firefox but not in ChromeAjax 在 Firefox 中有效,但在 Chrome 中无效
【发布时间】:2016-02-19 09:53:18
【问题描述】:

在 Firefox 中一切正常,所有数据都返回一个值,但在 chrome 上,它们都是未定义的。 我试图用一个简单的字符串返回 ex 来清空我的函数。 'asd' 这仍然返回未定义。 在 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);
        }
    });  
});

这是我的功能

function synergy_vote() {

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

}

【问题讨论】:

  • 如果在成功回调函数中使用 console.log(data) 会记录什么?
  • 好的,我不知道我必须登录,现在它可以工作了。你能解释一下为什么吗?看来我的朋友即使登录了也不能投票。
  • omvp_ajax.ajax_url 定义在哪里?
  • 和函数在同一个文件中,wordpress中的function.php wp_localize_script( 'ajax-script', 'omvp_ajax', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

标签: jquery json ajax google-chrome firefox


【解决方案1】:

尝试解析从服务器返回的 JSON。

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

【讨论】:

  • 发送错误。 SyntaxError:JSON.parse:JSON 数据的第 1 行第 2 列出现意外字符
【解决方案2】:

替换

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

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

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2020-04-04
    • 2011-03-16
    • 2015-06-11
    • 2018-07-05
    相关资源
    最近更新 更多