Javascript 如何在客户端修改GET请求(ajax之外)的accept头?

Javascript 如何在客户端修改GET请求(ajax之外)的accept头?,javascript,django,http-accept-header,Javascript,Django,Http Accept Header,我在一个页面上有一个“下载到Excel”按钮,其目的是调用完全相同的URL,但请求标题设置为application/ms Excel 目前,我正在假装它,通过调用另一个URL,然后调整标题,然后转发到同一个函数 服务器端Django: HTTP_HEADER_EXCEL = "application/ms-excel" #fake testing url #http://localhost:8000/myfunction/<CLASSID>/xls/ def myfunction

我在一个页面上有一个“下载到Excel”按钮,其目的是调用完全相同的URL,但请求标题设置为application/ms Excel

目前,我正在假装它,通过调用另一个URL,然后调整标题,然后转发到同一个函数

服务器端Django:

HTTP_HEADER_EXCEL = "application/ms-excel"

#fake testing url
#http://localhost:8000/myfunction/<CLASSID>/xls/

def myfunction_xls(request, CLASSID):
    #intercept request, add the appropriate accepts
    #and forward it
    request.META["HTTP_ACCEPT"] = HTTP_HEADER_EXCEL
    request.META["dbr"] = dbr

    return myfunction(request, CLASSID)

#standard url
#http://localhost:8000/myfunction/<CLASSID>/

def myfunction(request, CLASSID, f_callback=None):

    if request.META["HTTP_ACCEPT"] == HTTP_HEADER_EXCEL:
        f_callback=provider.generateExcel

    ....do lots of work...

    di_context = dict(inst=inst,
            parent=inst,
            custom=custom,
            url_excel=url_excel,

    if f_callback:
        #use xlsxwriter to process di_context data
        #wrap up the appropriate response headers
        #and it appears as a download  (it works)
        return f_callback(request, di_context)

    #non-Excel branch, i.e. standard Django behavior
    t = get_template('pssecurity/security_single.html')
    c = RequestContext(
        request,
        di_context,
        )    
    html = t.render(c)
    return HttpResponse(html)
我的问题是,我不想只为Excel维护自定义URL,也不想为URL的正则表达式添加可选的/xls/。使用现有的url并让服务器根据accepts头进行调整完全可以。是的,我可以添加查询参数来表示xls,但是。。。我的特殊要求不是accept头的用途吗

我找到了一个关于如何在Ajax中实现这一点的方法,但这里没有必要这样做。非常高兴有一个常规的GET-not-POST请求,它恰好指定了application/ms-excel

我知道我无法使用href属性指定接受。而且,虽然javascript中的window.open可以很好地实现这一点,但我也看不到任何方法可以更改那里的accept头


嗯,是的,这可能是一个网络noob问题,但我找不到太多关于在$http或$ajax欺骗之外轻松修改accept头的信息。

也许这会对通过下载属性将文件名添加到指向url的标记中有所帮助,然后它将在不使用特殊头的情况下下载。哪个url?带/xls/?如果accept header在ajax之外像它看起来一样有用的话,我就不需要它了。