0

I'm not very confident in AJAX unfortuantely,

Im looking to call the current post URL via ajax.

Do you have any idea how is this possible ?

So far I'm trying the following, no success . .

        $(document).on('click', '.send', function(e) {
        $.ajax({
            url: ajaxpagination.ajaxurl,
            type: 'POST',
            dataType: 'json',
            data: ({
              pageurl: the_slug(),
              userEmail: userEmail
            }),
            success: function( data ) {
                alert( 'Your home page hurl' + $(pageurl);
            }
        })

--EDIT --

From the comment of mmm I have added the script in functions.php and then callign it like below:

$(document).on('click', '.link', function(e) {



    var $this = $(this),
        category = $this.attr('data-event-category'),
        action = $this.attr('data-event-action'),
        label = $this.attr('data-event-label'),
        permalink =  object["permalink"]



    self.track(category, action, label);

            console.log('tracking pushed: '+category+', '+action+', '+permalink)

});

},

Doesn't seems to be sucessfull, I'm having an object is not defined . .

2
  • which data do you want to receive ?
    – mmm
    Commented Oct 25, 2017 at 17:06
  • The permalink (current post url) Commented Oct 25, 2017 at 17:15

1 Answer 1

1

you can retrieve the permalink of the current post in JavaScript with wp_localize_script.

juste try this code after the call of wp_enqueue_script

wp_localize_script(
      "script_id"
    , "object"
    , [
        "permalink" => get_post_permalink($post),
    ]
);

and then you can read the permalink in JavaScript in object["permalink"]

9
  • thanks, I have added the function, Then in a variable i add object["permalink"] Callng the variable then but it doesnt seem to work . . . I'm having gan object is not defined I'm updating the post if it helps . . Commented Oct 26, 2017 at 8:27
  • can you give us the URL of this page ?
    – mmm
    Commented Oct 26, 2017 at 9:45
  • its not live unfortnately . . .:( Commented Oct 26, 2017 at 9:48
  • search in the source code if you see the line var object =...
    – mmm
    Commented Oct 26, 2017 at 10:11
  • I don thave any bar object = ... just the code i have updated if this help ? what should be done ? Thanks a lot for your time Commented Oct 26, 2017 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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