ajax – randomly get 400 error while user is logged in wp_ajax

Question

use this for login

$result     = wp_signon( $credential, is_ssl() );

this code for retrive some data

function daryaft_etelaat_moshtari(shomare_mobile) {
    return new Promise(function (resolve, reject) {
        jQuery(document).ready(function () {
            jQuery.ajax({
                type: "POST",
                url: ajaxurl,
                data: {
                    'action': "daryaft_etelaat_moshtari",
                    'shomare_mobile': shomare_mobile
                },
                success: function (response) {
                    try {
                        resolve(response);
                    } catch (error) {
                        reject(error);
                    }
                },
                error: function () {
                    reject(new Error("Request failed"));
                }
            });
        });
    });
}

and this for fetch data

function namayesh_etelaat_moshtari(shomare_mobile) {
        jQuery('#loading_screen').show()
        daryaft_etelaat_moshtari(shomare_mobile)
            .then(function (data) {
                // Use the data object here
                jQuery('#loading_screen').hide()
                if (data) {
                    // set value
                }
            })
            .catch(function (error) {
                // Handle errors here
                jQuery('#loading_screen').hide()
                alert(error);
            });
    }

in backend use

add_action( 'wp_ajax_daryaft_etelaat_moshtari', 'daryaft_etelaat_moshtari' );

get randomly error 400 in some browser even if user is logged in

if got error on wp_ajax_daryaft_etelaat_moshtari then wp_ajax_nopriv_daryaft_etelaat_moshtari will work
and if wp_ajax_nopriv_daryaft_etelaat_moshtari error then wp_ajax_daryaft_etelaat_moshtari will work while in both way user is logged in

0
hossein khosromanesh 1 month 2023-04-29T14:21:23-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse