Bob
Bob

Reputation: 8724

Cakephp 2 jquery post request

I would like to make web service which will delete post asynchronusly. I am not sure how to create request in jquery.

This is how I am working now, and it doesn't work

$(".delete-link").live("click", function(e){
        e.preventDefault();
        var id = $(this).attr("id");
        $.post("app/posts/deleteAjax/", {"id": id}, function(data){
            console.log(data);
        });
    });

My link has class "delete-link". I created function deleteAjax with parameter ID in file PostsController.php

Cake gives me error that it can't find file. I am not sure how should I make a call properly and how to handle it

Upvotes: 0

Views: 487

Answers (1)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100205

Try giving full path to url like,

$.post("<?php echo $this->webroot; ?>posts/deleteAjax/", {"id": id}, function(data){
 ...

Added If js extension file, then you can define webroot in your layout.ctp and use it in your .js file, like in your default.ctp:

<script type="text/javascript">
var webroot = '<?php echo $this->webroot; ?>';
//now you can use webroot to get full path
</script>

Hope it helps

Upvotes: 1

Related Questions