IE9 decline response of http server - ajax

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.

Related

Ajax request error with elasticsearch

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) {
...
});

JS Cross-Domain request with JSONP

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);
},
});

Unable to download csv file even though content coming in response header

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).

Malformed JSON response being sent by Play Server

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".

Not able to send POST request through jQuery Ajax

I was trying to send an ajax request as POST request. But when i verified it on httpFox on firefox, the request is sent as GET. I tried both $.ajax() and $.post().
Many had a query regarding the same and had missed the "type" in $.ajax(), but even if i mention the type as "POST", the request will be of type GET.
Here is my code:
$('.test').click(function(){
alert("clicked");
$.ajax({
type: "POST",
url: "www.testsite.com",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
});
Any idea why it happens?
A possible cause might be the fact that you are trying to send an AJAX request to a different domain: www.testsite.com than the one hosting your page which of course is not possible and jQuery tries to use JSONP instead which works only with HTTP GET.

Resources