0

Is it possible to add another new dataTypes in AJAX using short-hand method POST in jquery? Maybe someone who's expert in jquery can help me.

This is my AJAX.

$.ajax({
   url: myUrl,
   type: 'POST',
   data: formData,
   cache: false,
   contentType: false,
   processData: false,
   success: function(data){
          console.log(data);
   }
});

I want to convert it to short-hand;

$.post(myURL,{
                    formData
}).done(function(response){
    console.log(response)
}, contentType: false, processData: false, cache: false
);

But the above code is not working.

aldrin27
  • 3,407
  • 3
  • 29
  • 43
  • 1
    $.post will not do this.see http://stackoverflow.com/questions/2845459/jquery-how-to-make-post-use-contenttype-application-json post it may help. – Suchit kumar Oct 27 '15 at 03:33
  • What's the point of using a short-hand method if you still want to specify all the same arguments? If you just want to replace `success` with `.done()` that will work with `$.ajax()`. – nnnnnn Oct 27 '15 at 03:36
  • @nnnnnn my point is how to add these `contentType: false, processData: false, cache: false` via short-hand method. – aldrin27 Oct 27 '15 at 03:38
  • @aldrin27 Will settings change between `$.ajax()` calls ? – guest271314 Oct 27 '15 at 03:40
  • @guest271314 Yes. But Suchit's comment give's me an idea. – aldrin27 Oct 27 '15 at 03:43

1 Answers1

0

You may create your own function and define any logics there:

$.superCustomAjax = function(settings) {
    settings.contentType = settings.contentType || false;
    settings.cache = settings.cache || false;
    settings.processData = settings.processData || false;

    return jQuery.ajax(settings);
});

$.superCustomAjax({
    url: "./url.php",
    data: data,
    cache: true // overwrite default "false" value
});

Or more $.post-style like this:

$.superCustomAjax = function(url, data, settings) {
    settings.url = url;
    settings.data = data;

    settings.type = settings.type || "POST";
    settings.contentType = settings.contentType || false;
    settings.cache = settings.cache || false;
    settings.processData = settings.processData || false;

    return jQuery.ajax(settings);
});

$.superCustomAjax("./url.php", data, {
    cache: true // overwrite default "false" value
});

In both examples, cache value will be overwritten to true. However, contentType and processData will be false according to your custom defaults.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101