Last active
April 16, 2016 15:28
-
-
Save ferdiunal/c2884bc732ff4f24d8fb841c653d30fa to your computer and use it in GitHub Desktop.
Laravel ile ajax işlemi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * index.blade.php'de jQuery'nin yüklü olması gerekiyor. | |
| * index.blade.php'de head tagları arasına | |
| * <meta name="_token" content="{!! csrf_token !!}" /> 'i ekliyoruz | |
| */ | |
| $.ajaxSetup({ | |
| // Ajax Post işlemi sırasında Laravel'in TokenMismatchException hatası vermemesi için | |
| 'datatype' :'json', | |
| 'method' : 'post', | |
| 'cache': false, | |
| 'headers' : { | |
| 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') | |
| } | |
| }); | |
| const post = { | |
| create : function(postId){ | |
| const $this = $(postId); | |
| if(!$this.length){ | |
| // Burada bir hata var is af buyura | |
| alert('Belirtilen id\'ye ait bir form bulunamadı !') | |
| return; | |
| } | |
| $.ajax({ | |
| data : $this.serialize(), | |
| url : $this.attr('action'), | |
| success : function(s){ | |
| // burada başarılı bir işlem gerçekleşirse ajax'tan sonra yapılacak işlemleri yapılır | |
| }, | |
| error : function(e){ | |
| // burada status kodlar dahil gerekli hata mesajları yakalanıp kullanıcıya iletilir, | |
| // Laravel'in validate hataları burada yakalıyacağız | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment