$.ajaxPrefilter() Vs $.ajaxSetup() - jQuery Ajax

后端 未结 2 683
南笙
南笙 2020-12-30 05:18

While learning through ajax in jQuery, I came across 2 terms, viz., $.ajaxPrefilter() and $.ajaxSetup(). All I can find out is that these

2条回答
  •  自闭症患者
    2020-12-30 06:03

    $.ajaxSetup() - Set default values for future Ajax requests. You could, for example, set the ajax URL that you always want to use for every request here.

    Example:

    $.ajaxSetup({
      // Always use this URL for every request
      url: "http://example.com/ajax.php"
    });
    

    $.ajaxPrefilter() - Modify existing options before each request is sent. You could, for example, append a query string component to every ajax request that is sent out.

    Example:

    $.ajaxPrefilter( function(options) {
        // Always add "?debug=1" to every URL
        options.url += (options.url.indexOf("?") < 0 ? : "?" : "&") + "debug=1";
    });
    

提交回复
热议问题