How to include header in ajax request?

前端 未结 3 1552
情话喂你
情话喂你 2021-01-24 18:37

I need to include a header with a refresh token in an ajax call to the YouTube api. I am trying to send a delete request, to delete a movie I have on my account. This is my ajax

相关标签:
3条回答
  • 2021-01-24 19:02

    You can use beforeSend method and request.setRequestHeader. Take a look at the official documentation here.

    P.S. should I post it as a comment?

    0 讨论(0)
  • 2021-01-24 19:02

    Try add the paramenter data.

    jQuery.ajax({
        type: 'DELETE',
        data: 'key=' + {YOUR_API_KEY},
        // must set api key
        url: 'https://www.googleapis.com/youtube/v3/videos?id='+ thisUniqueID + '&key=904907387177-qe517sq5dmmpebckjbmrhv4gvac9e2d1.apps.googleusercontent.com',
        success: function() {
        alert('your video has been deleted');
        },
        error: function() {
        alert('error processing your requst');
        }
    });
    
    0 讨论(0)
  • 2021-01-24 19:05

    I was able to pass along a header using this code below:

        jQuery.ajax({
            type: 'DELETE',
            // must set api key
            url: 'https://www.googleapis.com/youtube/v3/videos?id='+ thisUniqueID +'&key=api_key_here',
    beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'Bearer access_token_here');},
            success: function() {
            alert('your video has been deleted');
            },
            error: function() {
            alert('error processing your request');
            }
        }); 
    
    0 讨论(0)
提交回复
热议问题