dongou0524 2017-03-16 20:53
浏览 187

Wordpress Ajax始终返回0

So I am trying to make a form that validates if an url exists dynamically and am having problems with Wordpress ajax call ALWAYS returning 0. I have also been researching for about 3 hours now about this and nothing seems to work for me, it might be a dumb question. Here is what I have done so far:

I have a button:

<input id="analyze"type="button" class="analyze-button" value="Analyze">

And I am trying to run this script on it (located in another file):

$(document).on('click', '#analyze', function(){
    $.ajax({
        url: checkurl.ajax_url,    
        type:'POST',
        data: {
            action: 'checkUrl',
            //link_check : $('#main-input').val(),
        }, success: function(msg){
            alert(msg);
        }, error:function(msg){
            alert("Error");
        }        
    });
});

This is how I added the ajax hooks:

add_action( 'wp_ajax_nopriv_checkUrl', 'checkUrl' );
add_action( 'wp_ajax_post_checkUrl', 'checkUrl' );

function checkUrl(){
    echo "I works1";
    if(defined('DOING_AJAX') && DOING_AJAX){
        echo "I work";
    }

    echo "I works";
    die();
}

And this is how I enqueued the script:

function ajax_contact(){
    wp_enqueue_script('ajaxcontact', get_template_directory_uri() .'/templates/ajaxcontact.js', array('jquery'), '1.0', true);
    wp_localize_script('ajaxcontact', 'checkurl', array(
        'ajax_url' => admin_url( 'admin-ajax.php' )
    ));
}
add_action('wp_enqueue_scripts', 'ajax_contact');

Last 2 code snippets were from function.php

What am I doing wrong? I tried a lot of things, but with no luck so far.

This is the link to the page where I have the form. It is a test page right now. Maybe this helps.

  • 写回答

1条回答 默认 最新

  • drzrzzkh462254 2019-08-15 06:02
    关注
    // Don't forget to stop execution afterward.
    
    
      wp_die();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误