I have this stupid problem that I am seeing my csv data in the response header but it's not downloading the csv.
On client side, I have a button and on it's click an ajax post request is fired like
$.ajax({
url: 'xyz/GenerateCSV',
type: 'POST',
data: postData,
contentType: "application/json; charset=utf-8"
});
On the server side I have set Response as
Response.AddHeader("content-disposition", "attachment;filename=ListExport.csv");
Response.ContentType = "text/csv";
The http response header is coming fine as -
Cache-Control:private
content-disposition:attachment;filename=EncounterListExport.csv
Content-Encoding:gzip
Content-Type:text/csv; charset=utf-8
Date:Mon, 22 Sep 2014 14:18:05 GMT
Server:Microsoft-IIS/8.0
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:4.0
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?
Any idea what's going on? I did not make use of any Form or 'submit' button.
Thanks,
Vaibhav
Regarding your comment above:
It fails in the sense that nothing happens.
That's kind of incorrect. By your own admission, "Response content has data." Therefore something does happen. The response sends the data to the client.
The question is, what do you do with that data? According to your code, nothing:
$.ajax({
url: 'xyz/GenerateCSV',
type: 'POST',
data: postData,
contentType: "application/json; charset=utf-8"
});
Look at the documentation for $.ajax(). You need to provide it with some code to invoke after the response is received. That code would handle the response in some way. Something like this:
$.ajax({
url: 'xyz/GenerateCSV',
type: 'POST',
data: postData,
contentType: "application/json; charset=utf-8"
}).done(function (response) {
// use the data from the response
}).fail(function (response) {
// handle an error from the response
});
What you do with that data is up to you. It's structured CSV data, I imagine there exists some JavaScript library out there which can parse that easily. If you want to save the data to a file then you're going to have to prompt the user in some way (which could involve some trickery and workarounds) or save it to local storage. JavaScript can't write directly to the file system (for obvious security reasons).
Related
I simple make get req with ajax to elasticsearc to get all indices,
in browser it works here:
its same in my ajax, I expected the json data in browser return to my success method but failed:
Response is not JSON, you can change the response to a JSON in your server program.
It should work if you replace json by jsonp in your dataType parameter, like this:
$.ajax({
url: "http://localhost:9200/dicoms/dicoms/_search",
dataType: "jsonp", <--- change this
type: "GET",
success: function(data) {
...
});
I'm trying to perform a cross domain call. so i'm using JSONP.
The problem is that the response is not json, but an html.
The request works fine and i see the response with status 200 in the network console, however the error function is the one being called, as the expected response is JSON whereas I get an html.
Do you have alternatives for using JSONP for cross domain request?
If i'm using JSONP but the request is not, can I somehow expect a value which is not a json? (tried dataType: 'jsonp text' and didn't work)
Although I get an error with the ajax call, is there a way to extract the result of the request, as it still returns status 200, it's just not accessible ?
$.ajax
({
type: "GET",
url: myurl,
crossDomain:true,
dataType: 'jsonp',
contentType: "text/html; charset=utf-8",
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
},
});
IE9 decline response of below ajax request to local http server.
$.ajax({
type: 'GET',
url: "127.0.0.1",
data:{res:"pending"},
dataType: "text",
crossDomain: false,
cache: false,
success: success,
error: error
});
respone of server :
"HTTP/1.0 200 OK\r\n"
"Access-control-allow-methods: *\r\n"
"Access-control-allow-origin: *\r\n"
"Content-Type: text/html\r\n"
"Content-Length: 0\r\n\r\n"
Is there any reason???? what IE9 expects??
I would recommend to take a look at the jQuery contentType parameter and dataType as well. Note that possible values are xml, json, jsonp, text, script, html. The default is Intelligent Guess by jQuery.
As stated in documentation:
dataType: The type of data that you're expecting back from the server.
contentType: When sending data to the server, use this content-type.
You should specify that you are sending HTML type data (by using html as dataType, because your server returns HTML in the response header (Content-Type: text/html), also if you have script tags in returned result - those will be evaluated when inserted in the DOM.
I am using jquery Ajax to send data to the client and get reply from the Client.
I am using Play Framework as backend.
AJAX:
$.ajax({
type: "GET",
url: '/authenticate',
data: {'type':type, 'redirectURL':getRedirectURL},
contentType: "application/json; charset=UTF-8",
dataType: 'json'
}).success(function( msg, txtStatus, jqXHR) {
console.log("asdasd5= "+msg);
console.log("asdasd5= "+msg.authUrl);
console.log("asdasd5= "+jqXHR.authUrl);
window.location = msg;
});
SERVER DATA CREATION:
response.setContentTypeIfNotSet("text/plain; charset=UTF-8");
Logger.info("response content type ="+response.contentType);
renderJSON("{\"authUrl\": " + authUrl +"}");
The server is sending something like "www.mywebsite.com/yoohoo/auth/1231"
But the response received by Ajax is �����������������{"authUrl": www.mywebsite.com/yoohoo/auth/1231}
DUE to these weird character the JSON response received cannot be parsed by jQuery.
Kindly, help on this.
Thanks
I noted that you are sending back (server-side) a MIME type of "text/plain". Perhaps switch to "application/json". Additionally, you could take the return string and strip any junk from the string before parsing the JSON out on the client side. This would help sanitize in case you change something again server-side and accidentally introduce new/different "junk".
$(document).ready(function() {
$('#content').html('');
$.ajax({
url:'data.json',
dataType: "json",
success: function(data) {
$('#content').append('<p>'+data.rank+'</p>');
}
});});
In this code (it works) data.json contains the JSON data in this format:
{
"user_id":"3190399",
"user_name":"Anand_Dasgupta",
"followers_current":"86",
"date_updated":"2009-06-04",
"url":"",
"avatar":"205659924\/DSC09920_normal.JPG",
"follow_days":"0","started_followers":"86",
"growth_since":0,
"average_growth":"0",
"tomorrow":"86",
"next_month":"86",
"followers_yesterday":"86",
"rank":176184,
"followers_2w_ago":null,
"growth_since_2w":86,
"average_growth_2w":"6",
"tomorrow_2w":"92",
"next_month_2w":"266",
"followersperdate":[]
}
This data comes from the URL:
http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3
(Click the URL to get the data)
But when I replace data.json in the $.ajax function with the URL which contains the same data, this code below doesn't seem to work...
$(document).ready(function() {
$('#content').html('');
$.ajax({
url:'http://twittercounter.com/api/username=Anand_Dasgupta&output=json&results=3',
dataType: "json",
success: function(data) {
$('#content').append('<p>'+data.rank+'</p>');
}
});});
Any help with the problem will be highly appreciated.
Thanks in anticipation,
Anand
The thing is that you are trying to access an url on a different domain (unless you actually are on twittercounter.com of cause). Anyways, if you want to do cross-site AJAXcalls which browsers don't permit due to safety, you have to use the JSONP "trick". You can use JSONP with jQuery, which it seems like you are using. Last I checked though, jQuery required some server side configuration, so unless you can alter the data you get, you would have to do the AJAX request manually, in which case you would be able to get it, using the JSONP method.
Basically you are trying to access a cross-domain AJAX request. This is not allowed because it tends to compromise browser security. Here is a way that you can get around it:
http://code.google.com/p/cross-domain-ajax/
Noah
Edit: Mon Jun 29 10:24:51 CDT 2009 googletorp FTW!