ajaxflaskresponseresponse-headersjqxhr

Using flask, ajax - get response header value


I'm sending request through ajax, sending response through Flask.

This is my code

ajax

'''

$.ajax({
    url:'/get',
    type: "post",
    data: {"hi":"hello"},
    success(function(data, textStatus, jqXHR){
        console.log(jqXHR.getResponseHeader('token'));
    })
})

'''

Flask '''

@app.route('/get', methods=['POST'])
def hello():
    data = {'result': 0}
    resp = make_response(data)
    resp.headers['hi'] = 123
    return resp'''

If I run this, I can see response header ('hi', 123) in chrome inspection. But NO CONSOLE LOG.

Is there anything wrong with my code?


Solution

  • There's no token response header in your server response, try reading hi header:

    $.ajax({
        url:'/get',
        type: "post",
        data: {"hi":"hello"},
        success(function(data, textStatus, jqXHR){
            // this line: 
            console.log(jqXHR.getResponseHeader('hi'));
        })
    })