Last active
December 9, 2019 07:14
-
-
Save me7media/c516f50677976b251688632b036c6fd7 to your computer and use it in GitHub Desktop.
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
<meta name="csrf-token" content="{{ csrf_token() }}"> | |
//В main.js | |
$('.sendItAjax').on('click', function (e) { | |
e.preventDefault(); | |
var $this = $(this), | |
data = {}, | |
tmp = $this.data('data').split(','); | |
tmp.forEach(function (item) { | |
data[item] = $('#'+item).val() || $this.data(item) || null; | |
}); | |
$.ajax({ | |
headers: { | |
'X-CSRF-Token': $('meta[name="csrf-token').attr('content') | |
}, | |
url: $this.prop('href') || $this.data('href'), | |
method: $this.data('method') || 'GET', | |
data: data, | |
success: function (data, status, settings) { | |
let func = window[$this.data('func')]; | |
if (typeof func === "function") { | |
func(data, status, settings); | |
} | |
}, | |
error: function (err) { | |
alert('Ошибка, повторите позже!'); | |
} | |
}).then(function (data) { | |
let func = window[$this.data('then')]; | |
if (typeof func === "function") { | |
func(data); | |
} | |
}) | |
}); | |
// Применение на странице | |
<input id="user" value="" /> | |
<input id="password" value="" /> | |
<a class="sendItAjax btn btn-success" href="/" data-func="funcSuccessIt" >GO</a> | |
<button class="sendItAjax btn btn-success" data-href="/" data-func="funcSuccessIt" data-method="post" data-data="user,password"> | |
<i class="fas star"></i> | |
</button> | |
<script> | |
funcSuccessIt = function(data){ | |
$this.find('i.star').toggleClass('fas').toggleClass('far'); | |
alert(data); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment