How to pass custom header with ajax call in .Net MVC?

Forums ASP.NET MVCHow to pass custom header with ajax call in .Net MVC?
Staff asked 1 month ago

Answers (2)

Add Answer
Staff answered 1 month ago
$.ajax({
    url: "/test",
    headers: {"X-Test-Header": "test-value"}
});

 

Staff answered 1 month ago

Add the below code before your ajax call. This will set the header for all the ajax calls on your page. If you need to pass the same headers with multiple ajax calls then you can use this way.

$.ajaxSetup({
   beforeSend: function (xhr) {
      xhr.setRequestHeader('YOUR_HEADER_NAME', 'VALUE' );
      xhr.setRequestHeader('YOUR_HEADER_NAME_2', 'VALUE_2' );
   }
});

Subscribe

Select Categories