Eleazar Ortega
Eleazar Ortega

Reputation: 539

Jquery Ajax Bad Request 400 Wordpress Widget

I am trying to send a package to admin-ajax.php to save the info into a $_POST variable to handle it in cart page... But the .ajax only fail...

var sendJsonA = {'value':'Llegando desde JSON'}
var ajaxurl = $('#ele_admin-ajaxURL').attr('data-value') //this is is "https://blablabla.com.ve/wp-admin/admin-ajax.php" -->>> err 400 bad request
// var ajaxurl = '<?php echo admin_url("admin-ajax.php"); ?>'; doasnt work neither error 404 not found

		jQuery.ajax({
			data: sendJsonA,
			url:ajaxurl,
			type:'POST',
			dataType:'json',
			beforeSend: function(){
				console.log('Se envia : '+JSON.stringify(sendJsonA))
			},
			success: function(response){
				console.log('Se recibe : '+JSON.stringify(response))

			}
		})
		.done(function( data, textStatus, jqXHR ) {
			if ( console && console.log ) {
					console.log( "La solicitud se ha completado correctamente."+ JSON.stringify(jqXHR) );
			}
		})
		.fail(function( jqXHR, textStatus, errorThrown ) {
			if ( console && console.log ) {
				console.log( "La solicitud a fallado 2: " +  JSON.stringify(jqXHR)+"----------------");//this is the only message i see in the console

			}
		}); 
function ele_inSelectionGrid()
{
	echo "Mostrando texto antes de POST : ".$_POST['value'];
	if ( isset( $_POST['value'] ) ) {
		$numbers=$_POST['value'];
		session_start();
		$_SESSION['value']=$numbers:
	    die();
	}


};	
add_action('wp_ajax_ele_inSelectionGrid', 'ele_inSelectionGrid');
//this never happends

I dont know what to try...

Also I have to say that the security certificate of the host is expired for more than 100 days, every time I try to open the hosting it sends an error warning of unsafe site because it does not have a valid security certificate and it has not yet been renewed. This can intervene? Since ajax uses https. It happens that other woocommerce ajax applications work well because the console displays messages from finished XHR Loading from other applications

i dont know what else to try, to continue with this proyect.

Upvotes: 1

Views: 1226

Answers (1)

James
James

Reputation: 22247

You have to set the action property of the data you are sending via ajax, so that wordpress knows which wp_ajax_xxxx function to run.

For example in your case you want to call wp_ajax_ele_inSelectionGrid, so you must add a property "action": "ele_inSelectionGrid" to your data object, ie:

var sendJsonA = {'value':'Llegando desde JSON', 'action': 'ele_inSelectionGrid'};

There are some more examples in this question.

Upvotes: 1

Related Questions