• Resolved surmashigri

    (@surmashigri)


    
    //pluginname/index.php
    
    class _AjaxReviewLite_
    {
        protected $postId;
    
        /**
         * _AjaxReviewLite_.
         * @param $wp_query
         */
        public function __construct($wp_query)
        {
    
            
            
    
            if ($wp_query && $wp_query->post) {
                $this->postId = $wp_query->post->ID;
                $this->setHooks;
            }
        }
    
        /**
         * Get Post Id
         * @return mixed
         */
        public function getPostId()
        {
            return $this->postId;
        }
    
    
    
        public function _review_lite_resources_()
        {
            wp_enqueue_style('review-lite-css', plugins_url('/css/style_.css', __FILE__));
            wp_enqueue_script('review-lite-js', plugins_url('/js/review-lite_.js', __FILE__), array('jquery-core'), 1, 1, 1);
            wp_localize_script('review-lite-js', 'asyncit', array('ajax_url' => admin_url('admin-ajax.php')));
            
        }
    
    
        //define function to show output
        public function _review_lite_($atts, $content = '', $tag)
        {
    
            return require_once plugins_url('/review-lite/inc/require_.php');
        }
    
        public function setHooks(){
            //resources being enqued
            add_shortcode('review_lite_', [$this, '_review_lite_']);
            add_action('wp_enqueue_scripts', [$this, '_review_lite_resources_']);
            
            //ajaxcall back
            add_action('wp_ajax_functionreviewsubmit', [$this, 'functionreviewsubmit']);
            add_action('wp_ajax_nopriv_functionreviewsubmit', [$this, 'functionreviewsubmit']);
            add_action('wp_ajax_functionreviewget', [$this, 'functionreviewget']);
            add_action('wp_ajax_nopriv_functionreviewget', [$this, 'functionreviewget']);
        }
    }
    
    /**
     * Start plugin
     */
    add_action('wp', function () {
        global $wp_query;
        $_AjaxReviewLite_ = new _AjaxReviewLite_($wp_query);
    });
    
    
     //review-lite-js
    
                $.ajax({
                    url: asyncit.ajax_url,
                    method: "POST",
                    data: {
                        action: "functionreviewsubmit",
                        rating_data: rating_data,
                        user_name: user_name,
                        user_email: user_email,
                        user_review: user_review,
                    },
                    success: function (data) {
                        alert(data);
    
                    }
                });
            

    all of the ajax call back functions are defined i couldnt post it here its too long

    iam getting error 400 bad request after inspecting xmlhttp requests

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    .ajax() on its own doesn’t send data in a format that is compatible with PHP. .post() however does. Go figure. If you want to use .ajax(), data needs to be properly encoded. IIRC, encoding it as form data or JSON will work. Sorry, I’m a little fuzzy on the specifics, I just use .post() for myself ??

    Thread Starter surmashigri

    (@surmashigri)

    ok thanks i sort it out there was two issues wp_die() needs to be added at the end of callback functions and class object shouldnt be created inside WP hook

    functionreviewsubmit {

    wp_die()

    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Ajax Call Inside Plugin Class Status Code: 400 Bad Request’ is closed to new replies.