Add custom http header to all jQuery AJAX Requests

前端 未结 2 792
时光说笑
时光说笑 2021-01-01 14:06

Point of clarification: I don\'t have any problem adding a custom header to my jQuery ajax call, I want to have my custom header added to all ajax calls automatically.

2条回答
  •  旧巷少年郎
    2021-01-01 14:22

    You can use the ajax global event ajaxSend()

    $( document ).ajaxSend(function( event, jqxhr, settings ) {
        jqxhr.setRequestHeader(name, value)
    });
    

    Demo: Fiddle

提交回复
热议问题