JSON Post with Customized HTTPHeader Field

后端 未结 4 1535
后悔当初
后悔当初 2020-12-04 19:24

I have inherited some code that will eventually be part of an API call. Based on the existing code, the call is a post to retrieve JSON code with an access_token. While this

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 19:55

    What you posted has a syntax error, but it makes no difference as you cannot pass HTTP headers via $.post().

    Provided you're on jQuery version >= 1.5, switch to $.ajax() and pass the headers (docs) option. (If you're on an older version of jQuery, I will show you how to do it via the beforeSend option.)

    $.ajax({
        url: 'https://url.com',
        type: 'post',
        data: {
            access_token: 'XXXXXXXXXXXXXXXXXXX'
        },
        headers: {
            Header_Name_One: 'Header Value One',   //If your header name has spaces or any other char not appropriate
            "Header Name Two": 'Header Value Two'  //for object property name, use quoted notation shown in second
        },
        dataType: 'json',
        success: function (data) {
            console.info(data);
        }
    });
    

提交回复
热议问题