Access Control Request Headers, is added to header in AJAX request with jQuery

前端 未结 6 1368
不知归路
不知归路 2020-11-22 10:29

I would like to add a custom header to an AJAX POST request from jQuery.

I have tried this:

$.ajax({
    type: \'POST\',
    url: url,
    headers:          


        
6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 11:08

    This code below works for me. I always use only single quotes, and it works fine. I suggest you should use only single quotes or only double quotes, but not mixed up.

    $.ajax({
        url: 'YourRestEndPoint',
        headers: {
            'Authorization':'Basic xxxxxxxxxxxxx',
            'X-CSRF-TOKEN':'xxxxxxxxxxxxxxxxxxxx',
            'Content-Type':'application/json'
        },
        method: 'POST',
        dataType: 'json',
        data: YourData,
        success: function(data){
          console.log('succes: '+data);
        }
      });
    

提交回复
热议问题