Discussion:
Why is this ajax POST failing?
BenderisGreat
2013-11-11 21:47:04 UTC
Permalink
*Jquery / JS File:*

jQuery( document ).ready( function( $ ) {
$( '#post_like').click( function() {

var like_user_id = $( this ).attr('data-id')
var like_post_id = $( this ).attr('data-user')

var data = {
'action': 'my_post_like_action',
'like_post_id': like_post_id,
'like_user_id': like_user_id
}

$.ajax({
type: 'post',
url: myAjax.ajaxurl,
data: 'data',
success: function ( response ) {
if(response.type == "success") {

alert( 'Success!' );
}
else {
alert( 'Fail!' );

}
}
});
});
});


*Enqueue Script:*

add_action( 'init', 'my_script_enqueuer' );

function my_script_enqueuer() {
wp_register_script( "my_post_like_action",
WP_PLUGIN_URL.'/gogonow/js/post_like.js', array('jquery') );
wp_localize_script( 'my_post_like_action', 'myAjax', array( 'ajaxurl' =>
admin_url( 'admin-ajax.php' )));

wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'my_post_like_action' );

}


*the php function:*

add_action('wp_ajax_my_post_like_action', 'my_post_like_action');
add_action('wp_ajax_nopriv_my_post_like_action', 'my_post_like_action');

function my_post_like_action() {

// if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
// die ( 'Busted!');

$post_id = $_POST['like_post_id'];
$user_id = $_POST['like_user_id'];

$result = update_user_meta($user_id, 'liked_by', $user_id);
$response = array( 'success' => true );

wp_send_json_success($response);

}


*the button: *

<button type=&quot;button&quot; id=&quot;post_like&quot;
data-id=&quot;&lt;?php echo the_ID() ?>" data-user="<?php echo
get_current_user_id(); ?>" class="btn btn-success">Like This Post</button>






--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Why-is-this-ajax-POST-failing-tp42791.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Jeremy Simkins
2013-11-11 21:49:57 UTC
Permalink
At first glance it looks like you are mixing user and post id when assigning them on click event.
Jeffrey Nolte
2013-11-11 21:56:18 UTC
Permalink
Also - data looks like it is being passed 'data' as a string rather than the data object.

Jeffrey Nolte
Moxie Media Group, Inc.
594 Broadway #305
New York, NY 10012
Office: (646) 820-8540
Skype: jeffrey.nolte
Post by Jeremy Simkins
At first glance it looks like you are mixing user and post id when assigning them on click event.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Gregory Lancaster
2013-11-11 21:59:41 UTC
Permalink
Sorry guys I posted my code up before undoing a few things I was trying to
solve the problem. Turned out it was the mixed ids, and the response was
set to .type rather than just success or fail. Thank you for the
immediate replies. While I have you, can someone explain the benefit of
using nonce in the enqueue vs nonce in the field being submitted?
Post by Jeffrey Nolte
Also - data looks like it is being passed 'data' as a string rather than the data object.
Jeffrey Nolte
Moxie Media Group, Inc.
594 Broadway #305
New York, NY 10012
Office: (646) 820-8540
Skype: jeffrey.nolte
Post by Jeremy Simkins
At first glance it looks like you are mixing user and post id when
assigning them on click event.
Post by Jeremy Simkins
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
J.D. Grimes
2013-11-12 13:53:14 UTC
Permalink
Off the top of my head I can’t really think of any real benefit either way in this case.
Post by Gregory Lancaster
Sorry guys I posted my code up before undoing a few things I was trying to
solve the problem. Turned out it was the mixed ids, and the response was
set to .type rather than just success or fail. Thank you for the
immediate replies. While I have you, can someone explain the benefit of
using nonce in the enqueue vs nonce in the field being submitted?
Post by Jeffrey Nolte
Also - data looks like it is being passed 'data' as a string rather than
the data object.
Jeffrey Nolte
Moxie Media Group, Inc.
594 Broadway #305
New York, NY 10012
Office: (646) 820-8540
Skype: jeffrey.nolte
Post by Jeremy Simkins
At first glance it looks like you are mixing user and post id when
assigning them on click event.
Post by Jeremy Simkins
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Andrew Bartel
2013-11-11 21:50:36 UTC
Permalink
Hook it onto wp_enqueue_scripts instead of init.

You don't need to enqueue jquery, you already have it as a dependency.

Localize after enqueueing.

You're not passing a nonce so you're nonce check will fail.

-Andrew
Post by BenderisGreat
*Jquery / JS File:*
jQuery( document ).ready( function( $ ) {
$( '#post_like').click( function() {
var like_user_id = $( this ).attr('data-id')
var like_post_id = $( this ).attr('data-user')
var data = {
'action': 'my_post_like_action',
'like_post_id': like_post_id,
'like_user_id': like_user_id
}
$.ajax({
type: 'post',
url: myAjax.ajaxurl,
data: 'data',
success: function ( response ) {
if(response.type == "success") {
alert( 'Success!' );
}
else {
alert( 'Fail!' );
}
}
});
});
});
*Enqueue Script:*
add_action( 'init', 'my_script_enqueuer' );
function my_script_enqueuer() {
wp_register_script( "my_post_like_action",
WP_PLUGIN_URL.'/gogonow/js/post_like.js', array('jquery') );
wp_localize_script( 'my_post_like_action', 'myAjax', array( 'ajaxurl' =>
admin_url( 'admin-ajax.php' )));
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'my_post_like_action' );
}
*the php function:*
add_action('wp_ajax_my_post_like_action', 'my_post_like_action');
add_action('wp_ajax_nopriv_my_post_like_action', 'my_post_like_action');
function my_post_like_action() {
// if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
// die ( 'Busted!');
$post_id = $_POST['like_post_id'];
$user_id = $_POST['like_user_id'];
$result = update_user_meta($user_id, 'liked_by', $user_id);
$response = array( 'success' => true );
wp_send_json_success($response);
}
*the button: *
<button type=&quot;button&quot; id=&quot;post_like&quot;
data-id=&quot;&lt;?php echo the_ID() ?>" data-user="<?php echo
get_current_user_id(); ?>" class="btn btn-success">Like This Post</button>
--
http://wordpress-hackers.1065353.n5.nabble.com/Why-is-this-ajax-POST-failing-tp42791.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Continue reading on narkive:
Loading...