How can I set the Accept Header for a link on a web page?

后端 未结 3 1387
甜味超标
甜味超标 2020-12-15 04:45

This is mostly an ivory tower question, since I can easily just make a new URL endpoint. But basically, I\'d like to be able to serve up CSV when the user has the Accept he

3条回答
  •  隐瞒了意图╮
    2020-12-15 05:25

    For those still interested, there is a way to do this in pure javascript.

    The following code uses JQuery (https://jquery.com/) and FileSaver.js (http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js) though you could write the respective parts yourself:

    //in case of non binary data use:
    var type = 'text/xml';
    var url = 'http://your_url_here/'
    $.ajax({accepts:{text:type},
            url:url,
            processData:false, 
            dataType:'text', 
            success:function(data){             
                    saveAs(new Blob([data], {type: type}),'filename.txt');                                                                  
                }, 
            error: function(){
                   // Handle errors here
                }
        });
    

提交回复
热议问题