$.ajax call function 400 Bad Request
-
Evening all,
The following issue I have read throughout the forums and elsewhere online but nothing so far seems to fixes the persistent issue.
I am currently create a custom plugin to extend the now un-maintained plguin Image Store for a client. I wanted to use ajax to call a function which is dynamically designed to create, update and delete custom terms, add term meta and a post with the necessary shortcode without my client having to chop and change through out the admin panel.
If I use the API to carry out this task it takes a bit of time to process on the server as well as doesn’t work 100% of the time.
The Error Message:
POST https://{client-url}/wp-admin/admin-ajax.php 400 (Bad Request)
Plugin Init:
require '_inc/ims_ca_functions.php'; add_action('admin_enqueue_scripts', 'admin_scripts'); function admin_scripts(){ wp_enqueue_script( 'ims_ca_core', plugin_dir_url( __FILE__ ) . '_assets/ims_ca_core.js', array(), '1.0.0', false ); wp_localize_script( 'ims_ca_core', 'ims_ca_rest', array( 'rest_url' => get_site_url().'/wp-json', 'nonce' => wp_create_nonce( 'wp_rest' ), 'ajaxurl' => admin_url('admin-ajax.php') ) ); } add_action('wp_ajax_ims_ca_test-ajax', 'imsCAAjaxRequest'); add_action('wp_ajax_nopriv_ims_ca_add_to_term', 'imsCAAjaxRequest');
The Function to Call:
function imsCAAjaxRequest(){ // Ajax Method Request $mode = $_POST['mode']; //POST, UPDATE, DELETE, GET // IMS_ALBUM Detail Request $taxID = 'ims_album'; $termID = $_POST['term_ID']; $termTitle = $_POST['term_Title']; $termSlug = Optimiser::uglify($termTitle); // Post Detail Request $catID = 59; $postID = $_POST['post_ID']; $postTitle = $termTitle; $postSlug = $termSlug; $postContent = '[image-store album='.$termID.' all=true]'; $postStatus = 'Publish'; $postType = 'post'; switch ($mode){ case 'POST': //First Insert New Term wp_insert_term( $termTitle, $taxID, array( 'description' => '', 'slug' => $termSlug, 'parent' => 0 ) ); //Second Insert New Post $postARGS = array( 'post_title' => $postTitle, 'post_type' => $postType, 'post_status' => $postStatus, 'comment_status' => 'closed', 'post_category' => $catID, 'post_content' => $postContent ); wp_insert_post( $postARGS ); //Third Insert Term Meta $newTerm = get_term_by( 'slug', $termSlug, $taxID ); $newPostSlug = get_post_field( 'post_name', get_post() ); if ($newPostSlug == $newTerm): $newPID = $newPostSlug->id; endif; add_term_meta( $newTerm->term_id, 'ims_ca_term-pid', $newPID, true ); // Finally Create and Encode JSON Array $response = [ 'Message' => 'Successfully added new album: '.$termTitle, 'Album ID' => $newTerm->term_id, 'Post ID' => $newPID ]; wp_reset_postdata(); return json_encode($response); break; case 'UPDATE': //First Update Term wp_update_term( $termID, $taxID, array( 'name' => $termName, 'slug' => $termSlug ) ); //First Update Post $postARGS = array( 'ID' => $postID, 'post_title' => $postTitle, 'post_slug' => $postSlug ); wp_update_post( $postARGS ); break; } wp_die(); }
The jQuery:
$(".testAJAX").on('click', function(e){ jQuery.ajax({ url : ims_ca_rest.ajaxurl, method: "POST", data:{ action: 'imsCAAjaxRequest', mode: 'POST', term_Title: 'Test Term Alpha' }, beforeSend: function ( xhr ) { xhr.setRequestHeader( 'X-WP-Nonce', ims_ca_rest.nonce ); }, success : function( response ) { console.log(response); } }); e.preventDefault(); })
I am now at a loss and any help is much appreciated.
Regards,
Stephen
- The topic ‘$.ajax call function 400 Bad Request’ is closed to new replies.