How to create a page with a lot of ajax calls?

I would like to ask how to create a page with a lot of ajax calls. for example, 10 ajax calls on one page that have a CRUD function ..

I currently have such a custom setup:

$('[data-ajaxform]').submit(function(event) {
event.preventDefault();
event.stopImmediatePropagation();

if(!$(this).parsley().validate()){
    return false;
}

var $this = $(this),
    url = $this.attr('action'),
    method = $this.attr('method'),
    container = $this.data('container');

if(typeof container !== undefined && container !== false)
    container = '.panel-body';

$.ajax({
    url: url,
    type: method,
    dataType: 'json',
    data: $this.serialize(),
    beforeSend: function(){
        $this.find(container).append('<div class="indicator show"><span class="spinner spinner1"></span></div>')
    },
})
.done(function(data) {
    setTimeout(function() {
        $this.find(container).prepend(data.responseText);
    }, 1000);
    $this.trigger('reset');
    hasValue($this.data('ajaxform'))
})
.fail(function(data) {
    setTimeout(function() {
        $this.find(container).prepend(data.responseText);
    }, 1000);
})
.always(function(data) {
    setTimeout(function() {
        $this.find('.indicator').remove();
    }, 1000);
});

return false;
});

Can you guys suggest me what good practice to implement with this or some articles that can help me? Many thanks

+4
source share
1 answer

, . , , , jQuery .ajax() , / , .

function myajax(data, callback /*or wrap all stuff in an object literal*/) {
  //any preprocessing or conditionals
  //for example put your setting the url, method, container here
  $.ajax({
    url: url,
    type: type,
    data: data,
    success: callback
  });
}

, .ajax() , .

0

Source: https://habr.com/ru/post/1606918/


All Articles