Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm having a bit of an issue with an ajax call on a site I've just taken over.

I've used similar calls before on other sites so not sure if it's a plugin restriction or conflict but any ideas are welcome!

The call is returning the full content of the page rather than the content the id of the post which is what I expected. I have added an alert to the js before the ajax call and the "thisPost" var is set correctly. I've tried a variety of different methods but none seem to let me return the post_id as a json object. In the end the return will be a json object with more details than just the post_id but cut it down till I get it working.

functions.php

function add_calendar_scripts(){
    if (is_page_template('template-eventsplanner.php') ) {
        wp_register_script('calendar', ( get_bloginfo('template_url') . '/js/calendar.js'), array ('jquery'));
        wp_enqueue_script('calendar');
        wp_localize_script( 'calendar', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    }
}

add_action('wp_head', 'add_calendar_scripts');

function get_full_event_callback(){
    global $wpdb;
    $response = array();
    $post_id = intval($_POST['posted']);
    $response[] = array('id'=>$post_id);
    $result = json_encode($response);
    echo "post_id";
    die();
}
add_action( 'wp_ajax_nopriv_get_full_event', 'get_full_event_callback' );
add_action( 'wp_ajax_get_full_event', 'get_full_event_callback' );

calendar.js

jQuery(document).ready(function() {
    var container = jQuery('body'),
        scrollTo = jQuery('.today');
    container.animate({
        scrollTop: scrollTo.offset().top - container.offset().top + container.scrollTop() - 120
    });
    Shadowbox.init({
        skipSetup: true
    });
    jQuery('.event_box').click( function(){
        var pretext = "<h1>Davis Track Hire Job Information</h1>";
        var thisPost= jQuery(this).data("event");
        var contents, pdf_button;
        var data = {
            action: 'get_full_event',
            dataType: 'json',
            posted: thisPost,
        };
        jQuery.post(MyAjax.ajax_url, data, function(response) {
            if(response!=0){            
                contents = pretext.concat(response);
                pdf_button = "<a href='#'>Link to PDF to follow</a>";
                // open a welcome message as soon as the window loads
                Shadowbox.open({
                    content:    contents.concat(pdf_button),
                    player:     "html",
                    title:      "Event Details",
                    height:     600,
                    width:      500
                });
            }
        });
    });
});
share|improve this question
    
What exactly does the page return? What you mean by "the full page code"? The callback itself should return a string literal-- "post_id". Please edit your code into the question. A question should not depend on a third party site. –  s_ha_dum Aug 23 '13 at 14:15
    
The callback response is returning the full html for the page it is called on, everything from the opening <!DOCTYPE HTML> to the closing tag. I'll edit the question to include the code now. edit: thanks for doing that, beat me to it! Also I know it's returning a string atm, this was just for testing to see if it was actually picking up the returned value, it doesn't seem to be. –  iamdannywilson Aug 23 '13 at 14:56

1 Answer 1

My issue was simple;

jQuery.post(MyAjax.**ajax_url**, data, function(response) {

was meant to be:

jQuery.post(MyAjax.**ajaxurl**, data, function(response) {

problem solved... sorry

share|improve this answer
    
Thanks made same mistake :p –  Rahil Wazir Oct 5 '13 at 12:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.