Cuong Le Cuong Le - 3 months ago 45
jQuery Question

How to add Accept Header with text/csv in jQuery ajax

This might be a simple question but I have not yet found the way to sort it out. I would like to download csv file from server (implemented by ASP.NET Web API) by ajax call:

type: "GET",
accepts: "text/csv; charset=utf-8",
url: "/api/employees",
success: function (data) {

I have put the Accepts header in jQuery ajax like above. But from fiddler, I see:

enter image description here

The Accepts header now turns to
Accept: undefined
. If I try to put:

accepts: {
csv: "text/csv; charset=utf-8"

The Accepts Header now turns to:
Accept: */*

enter image description here

So I guess that is the main point to make my server returns JSON object rather than CSV file.

How can I make Accepts Header in jQuery ajax correctly? and see in the fiddler should be:
Accept: text/csv

I am using Chrome.



    headers: { 
        Accept : "text/csv; charset=utf-8",
        "Content-Type": "text/csv; charset=utf-8"