jquery getResponseHeader always returns 'undefined'?

后端 未结 3 700
失恋的感觉
失恋的感觉 2021-01-11 09:27

I have a a form that I am submitting via ajax. I am using the jquery form plugin. What I am trying to do is get the \'Location\' header which is returned from my server.

3条回答
  •  心在旅途
    2021-01-11 10:08

    I'm doing something similar using the rails/rest way of returning a 201 "created" with a Location header to the new object and an empty body. jQuery's ajax method will throw a "parseerror" when encountering this since its expecting json but getting nothing back. I simply catch the 201 redirect in my error callback like so:

    function request_error(req, textStatus, errorThrown) 
    {
        if (req.status == 201 ) {
            var created_loc = req.getResponseHeader('Location');
            console.log('(201) created: ' + created_loc);
    
            // ... manual redirect here i.e. issue another ajax request to created_loc
            return;
        }
    
        // ... handle an actual error here
    }
    

    hope this helps!

提交回复
热议问题