JQuery Code:

jQuery("#dmhsc-form").on("submit", function () {
    var form = this;

    if (jQuery("input[name='url']", form).val() == "") {

        jQuery("div[id='results']", form).html('<div class="not-available">Please, Enter site address.</div>');
        return false;
    }

    var url = jQuery("input[name='url']", form).val();

    jQuery("div[id='results']", form).css('display', 'none');
    jQuery("div[id='results']", form).html('');
    jQuery("div[id='loading']", form).css('display', 'inline');

    var data = {
        'action': 'dmhsc_show',
        'url': url,
        'security': badnc_ajax.dmhsc_nonce
    };

    jQuery.post(badnc_ajax.ajaxurl, data, function (response) {
        var x = JSON.parse(response);
        if (x.status >= 0) {
            display = x.text;
        } else {
            display = "Error occured." + x;
        }
        jQuery("div[id='results']", form).css('display', 'block');
        jQuery("div[id='loading']", form).css('display', 'none');
        jQuery("div[id='results']", form).html(unescape(display));

    });
    return false;
});

Plugin File

function badnc_load_styles() {

wp_enqueue_script( 'badnc-script', plugins_url( 'script.js', __FILE__ ), array('jquery')); // script.js contains jquery code

wp_localize_script( 'badnc-script', 'badnc_ajax', array(
    'ajaxurl'       => admin_url( 'admin-ajax.php' ),
    'dmhsc_nonce'     => wp_create_nonce( 'dmhsc_nonce' ))
);
}
add_action( 'wp_loaded', 'badnc_load_styles' );
add_action( 'admin_enqueue_scripts', 'badnc_load_styles' );

function dmhsc_show() {

check_ajax_referer( 'dmhsc_nonce', 'security' );

if(isset($_POST['url']))
{
   $url = sanitize_text_field($_POST['url']);

    if(!$url || !is_string($url) || ! preg_match('/^(https?://)?(www.)?w+.[a-z]{2,6}(/)?$/i', $url)) {

      $result = array('status'=>0,'url'=>$url, 'text'=> '<div class="not-available">Please, Enter valid domain name.</div>');
      echo json_encode($result);

   } else {

/*$result = array('status'=>0, 'text'=> '<div class="not-available">Domain: '.$url.'</div>');  
echo json_encode($result);*/  //It display the message without calling to fucntion 

        check_https($url); //function call
   }    

}

die();
}

add_action('wp_ajax_dmhsc_show','dmhsc_show');
add_action('wp_ajax_nopriv_dmhsc_show','dmhsc_show');

function check_https($host) {

    $result = array('status'=>0, 'text'=> '<div class="not-available">Domain: '.$host.'</div>');
echo json_encode($result);
}

Problem
I want to show “Domain: domain name” when I submit the form. Form contains url field and submit button.
but I don’t know it is not displaying the message written in check_https() function.

I am also getting error in console:

Uncaught SyntaxError: Unexpected token { in JSON at position 74
at JSON.parse (<anonymous>)

If I display the message without calling to function check_https(), it works.

What should be the problem.

Read more here: wordpress plugin jquery ajax call to function not working


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: