0

I am using a very simple form request to and verify it through the wp_verify_nonce() function. It is working perfectly on the desktop. But not working on mobile devices( iPhone, Andriod ). Could someone help me to identify the issue?

HTML Code

<form action="" method="POST" id="test-form" novalidate="novalidate">
    <?php wp_nonce_field( 'irw_gzip_action', 'irw_gzip_field' ); ?>
</form>

JavaScript

var $self = $(this);
var $nonce = $('#irw_gzip_field', $self).val();

$.ajax({
    url: WT.ajaxurl, 
    type: 'POST', 
    datatype: 'json',
    data: {
        _ajax_nonce: $nonce,
        action: 'check_action'
    }
})

PHP Code

add_action( 'wp_ajax_nopriv_check_domain', array( $this, 'handle_domain_request' ) );
add_action( 'wp_ajax_check_domain', array( $this, 'handle_domain_request' ) );

public function handle_domain_request() {

    if ( ! isset( $_POST['_ajax_nonce'] ) || 
        ! wp_verify_nonce( $_POST['_ajax_nonce'], 'irw_gzip_action' )
    ) {
        return wp_send_json( array(
            'error' => 'Un-Authorized request!'
        ), 401);
    }
} 

Complete Codes:

Javascript: https://pastebin.com/fKGMgHD6

HTML: https://pastebin.com/ZrVegyNy

PHP: https://pastebin.com/abJe48ax

5
  • 1
    You're using admin-ajax.php, right? And can you be more specific on how does the wp_verify_nonce() not work on the mobile devices? Were you logged-in on all the devices you tested - if not, then you're probably not using the wp_ajax_nopriv_check_action hook. – Sally CJ Jul 18 '20 at 13:36
  • @SallyCJ I am using this code in the frontend and i use both wp_ajax_{$action} and wp_ajax_nopriv_{$action}. It is also strange behaviour for me. I checked the value is of the nonce successfully passed to the wp_verify_nonce but its not validating it. – jogesh_pi Jul 19 '20 at 1:11
  • After making the AJAX request, what's the output on the front-end when using the mobile devices? Also, is that your whole AJAX script? If not, can we see the entire code? – Sally CJ Jul 19 '20 at 1:39
  • @SallyCJ, I am always getting the Un-Authorized request! response on mobile frontend but on desktop its working fine. I have updated the question with the complete codes link. – jogesh_pi Jul 19 '20 at 2:04
  • Your code seems fine to me, so if you could set up a test site where I can test your form/AJAX, then I might be able to help you further or identify the cause to the issue. – Sally CJ Jul 19 '20 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.