wp query – WordPress AJAX Request returns 400

Question

I am trying to make a ajax post request, however I a getting a 400 error, not sure what I am doing wrong. If you are able to have a look at my code, I’d really appreciate it.

Below is the form.

<form class="add_shipping_method" method="post"  action="javascript:void 0" data-action="<?php echo get_home_url();?>/wp-admin/admin-ajax.php">
<div class="form-field">    
   
    <input name="cost" type="text" placeholder="Enter Cost" required>
</div>
<div class="form-field">    
   
    <input name="title"  type="text" placeholder="Enter Shipping Method Title" required>
</div>

<input type="hidden" name="submitted" id="submitted" value="true" />
<input type="hidden" name="action" value="cs_add_shipping_method"/>
 <button type="submit">Save Shipping Cost!</button>

Php Code

add_action("wp_ajax_cs_add_shipping_method","cs_add_shipping_method");
add_action("wp_ajax_nopriv_cs_add_shipping_method","cs_add_shipping_method");
function cs_add_shipping_method(){


        
           $vendor_id =  get_current_user_id();
     
    $table="wpzy_dokan_shipping_zone_methods";
      
    $data = array(
        'method_id' => $title,
        'zone_id'=>   '1',
        'seller_id' => $vendor_id,
        'is_enabled' => '1', 
        'settings' => serialize($general_settings),
        
    );
 

      $format = array('%s','%d');

    $success = $wpdb->insert($table, $data, $format);

    die;
}

// Here’s my Javascript Code

 // Save Shipping method Data
jQuery(document).on("submit", ".add_shipping_method", function (e) {
    e.preventDefault();
    var obj = jQuery(this);
    jQuery.ajax({
        url: obj.attr("data-action"),
        method: 'post',
        type: 'json',
        dataType: 'json',
        data: obj.serialize(),
        success: function (res) {
            console.log(res);
        },
        error: function (err) {
            console.log(err);
        }
    })
});

0
Jacob Peattie 7 months 2022-01-31T22:33:58-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse