0

Using Lumen, I'm displaying "Tickets". Here's my Tickets.blade.php:

@foreach($tickets as $ticket)
<div>
    <p>{{ $ticket->id }}</p>
    <p>{{ $ticket->content }}</p>
    <button onclick="deleteTicket({{$ticket->id}})">Delete</button>
</div>
@endforeach

So essentially, each time someone clicks delete, it triggers this script:

<script>
    function deleteTicket(id) {
        jQuery.ajax({
            type: "post",
            url: "/tickets/deleteTicket",
            data: id,
            dataType: "text",
            success: function () {
                console.log('deleted!');
            }
        });

    }
</script>

In my routes.php, I have this:

$app->post('tickets/deleteTicket','TicketsController@deleteTicket');

Finally, in my TicketsController.php, I have:

public function deleteTicket($id) {
    $ticket = Ticket::find($id);
    $ticket->delete();

    return redirect('/tickets');
}

The problem I'm getting when I press the button (console):

POST http://example.com/tickets/deleteTicket 404 (Not Found)

I don't understand what I'm doing wrong, and why that method isn't being found. Can anyone help out?

EDIT- I have changed my routes.php to:

$app->get('tickets/deleteTicket/{id}','TicketsController@deleteTicket');

My script looks the same but I changed the "type" to get rather than post.

If I visit, this site: http://mysite/tickets/deleteTicket/1, the ticket will be deleted, and it redirects to tickets page. But if the button is clicked, this error happens:

http://MYSITE/tickets/deleteTicket?id=3 404 (Not Found)

At this point, I'm thinking that I just need to revise my AJAX call to the correct URL, but I want to make sure security is an issue.

LatentDenis
  • 2,839
  • 12
  • 48
  • 99

2 Answers2

2

Or you can try this :

routes.php

$app->get('tickets/deleteTicket/{id}','TicketsController@deleteTicket');

Okay here's your blade : add class to button and remove the function on click. like this one :

<button class="delete" data-id="{{$ticket->id}}>Delete</button>

And your script :

$(".delete").on("click", function(e) {
     e.preventDefault();
    var id = $(this).data('id');
    $.ajax({ 
       url : "tickets/deleteTicket/"+id+"",
       type : "get" or "post",
      success: function(data) {
          alert(data);    
       }
    });
});
  • Can you return first the $id in your deleteTicket function. Like this public function deleteTicket($id) { return $id; } To check if there's really an ID passed through the route –  Aug 19 '16 at 06:13
  • Yes. What I did instead was `console.log($id);` inside the `deleteTicket` function, the id is being passed through. – LatentDenis Aug 19 '16 at 06:16
  • Hmm. Cann you change your url to url : "tickets/deleteTicket". I've removed the first forward slash. –  Aug 19 '16 at 06:17
  • That is the way it's been this entire time. Still no change. – LatentDenis Aug 19 '16 at 06:18
  • 1
    Try this in your routes.php : $app->get('tickets/deleteTicket/{id}, 'TicketController@deleteTicket'); I've tried on my pc and it works. –  Aug 19 '16 at 06:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121309/discussion-between-ruther-bergonia-and-volcovmeter). –  Aug 19 '16 at 06:28
  • I just tried that and it didn't work, still have a 404. Also, you just put `TicketController` instead of `TicketsController` – LatentDenis Aug 19 '16 at 06:29
  • 1
    have you changed the type in ur ajax function? type : 'get' and still in your route $app->get('tickets/deleteTicket/{id}, 'TicketsController@deleteTicket'); –  Aug 19 '16 at 06:38
  • 1
    to be sure, access the url directly in your browser for example : www.sample.com/tickets/deleteTicket/3 -> 3 here is the ticket id, replace it with the any id that you have in your database from tickets table Just make sure, you followed the code I gave you. –  Aug 19 '16 at 06:40
  • When I changed the ajax function to get as well, I get same error, but different URL: `http://IPADDRESS/tickets/deleteTicket?id=1` – LatentDenis Aug 19 '16 at 06:42
  • Can you give the full url for that? So I can test it here and see the actual errors. If you would allow. Thanks And what's the error when you're accessing it directly in the browser? –  Aug 19 '16 at 06:43
  • Boom. I just edited the question. Please take a look. I think there's some progress. – LatentDenis Aug 19 '16 at 06:55
  • Look as if, I type out `http://MYSITE/tickets/deleteTicket/3` it works, but if I change my AJAX to `url: "/tickets/deleteTicket/" + id;`, the URL if not found in the get call or post call. – LatentDenis Aug 19 '16 at 07:04
  • 1
    Change it to like this url : "/tickets/deleteTicket/"+id+" ", –  Aug 19 '16 at 07:07
0

Too things you must to change
- In JS

url: "/tickets/deleteTicket", // Try change to your full URL with http://
data: id, // Change to data: "id="+id,

- In Controller

public function deleteTicket(Request $request) {
    $ticket = Ticket::find($request->id);
    $ticket->delete();

    return redirect('/tickets');
}
KmasterYC
  • 2,294
  • 11
  • 19
  • I just tried changing to full URL with the `data` adjustment, still not finding that URL. – LatentDenis Aug 19 '16 at 06:32
  • Change POST to GET and access directly from browser to make sure everything work fine except the url – KmasterYC Aug 19 '16 at 06:35
  • Can you please give me an example, this is an IP address. – LatentDenis Aug 19 '16 at 06:36
  • Change route to `$app->get('/tickets/deleteTicket','TicketsController@delete‌​Ticket');`, write something in Controller and access `http://example.com/tickets/deleteTicket ` directly through browser – KmasterYC Aug 19 '16 at 06:40