Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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:

$.ajax({
    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.

like image 872
cuongle Avatar asked Mar 11 '13 07:03

cuongle


1 Answers

try

$.ajax({
    headers: { 
        Accept : "text/csv; charset=utf-8",
        "Content-Type": "text/csv; charset=utf-8"
    }
...
like image 99
Dakait Avatar answered Sep 23 '22 02:09

Dakait