Ajax call fails in Chrome, but works in Firefox - javascript

Any idea why the following Ajax call fails in Chrome, but not Firefox?
$.ajax({
type: "GET",
contentType: "text/csv; charset=utf-8",
dataType: "text",
cache: false,
url: "http://127.0.0.1:8080/param1/param2?param3=csv&otherParams=type1,type2,type3"
})
.done(function(data) {
console.debug("Data received from ajax call: " + data);
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.error("Data request failed (" + textStatus + "): " + errorThrown);
console.debug(jqXHR.responseText);
});
The URL that I am calling should return the data in csv format. I'm using jQuery 1.8.2. This works in Firefox, but not Chrome for some reason. The error message that I print out is:
Data request failed (text/csv): text/csv
Interestingly, in Chrome I can see the data returned in the jqXHR.responseText, so I can't figure out why it's throwing an error. I assume it has something to do with my not specifying the csv format properly, but I thought setting the dataType or contentType would fix that. What am I missing?
I realize this is a common problem, but in spite of my Googling and searching around Stack Overflow, I have been unable to find a solution that fixes this problem for me. All the suggestions I've found have said to set the contentType, the dataType, or the cache to false. As you can see, none of these solutions has worked for me. Your help is greatly appreciated!

Related

JSONP Parse Error with valid JSON Output

I am calling a Cross Domain AJAX Request using JSONP. Now, i am able to make it work using CORS. But, i want to know why it is not happening with JSONP. I looked at other threads with similar problem but couldnt figure out why it is not working in my case.
Here is the code:
$.ajax({
type: "GET",
url: http://XXXX:8000/sap/bc/test_rest/jsonp_test?mode=S&ticket=123,
dataType: "jsonp",
jsonp: false,
jsonpCallback: "myJsonMethod",
success: function (data) {
alert(data);
},
error: function (httpReq, status, exception) {
alert(status + " " + exception);
}
});
Now, this calls my server, the data is populated and then i get an alert "parse error myJSONMethod was not called" on a callback URL http://xxxx:8000/sap/bc/test_rest/jsonp_test?mode=S&ticket=123&_=1470322282936
Additionally, in the console i get the error as Uncaught SyntaxError: Unexpected token :
The response structure is:
{"ROOT":{"CONTRACT":"40002356","ITEM":"000010","KUNNR":"0000004676","NAME":"REALTY EXECUTIVES","NET_PRICE":19.95,"GROSS_PRICE":19.95,"MATNR":"144","SQFEET":""}}
When i run this JSON Output on jsonlint it says it is a valid JSON. I don't know where the JSON is getting messed up.
Your data must be like this to be valid for JSONP:
myJsonMethod({"ROOT":{"CONTRACT":"40002356","ITEM":"000010","KUNNR":"0000004676","NAME":"REALTY EXECUTIVES","NET_PRICE":19.95,"GROSS_PRICE":19.95,"MATNR":"144","SQFEET":""}});
the responsetext must be valid js code and it will run immediately when the response is over.So if you write your code
{"ROOT":{"CONTRACT":"40002356","ITEM":"000010","KUNNR":"0000004676","NAME":"REALTY
EXECUTIVES","NET_PRICE":19.95,"GROSS_PRICE":19.95,"MATNR":"144","SQFEET":""}}
in a <script> tag,the console will throw an err like that

jQuery.ajax doesn't detect http 500 error in internet explorer

I'm using jquery to make an AJAX call that returns an HTTP 500 error. Works fine in firefox, but IE reports a 200 error. I know the PHP is right, because if I go to the page directly, it shows up as a 500 error. It's just the jQuery part that doesn't work in IE.
Here's the ajax call:
$.ajax({
url: "download.php",
data: {
removefile: filename
},
type: "GET",
dataType: "html",
success: function(html) {
alert("Success");
},
error: function(xhr, status, errorThrown) {
alert("Error: " + xhr.status);
},
complete: function(xhr, status) {
alert("Complete: " + xhr.status);
}
});
The php I'm using is something like:
header($_SERVER['SERVER_PROTOCOL'] . " 500 Internal Server Error", true, 500);
echo "Remove failed";
I'm testing on IE 9, but tried all of the different compatibility modes with the same issue.
Any ideas?
Duh! Changing the request type from "GET" to "POST" solves the problem. Guessing IE was caching the response. Not the same question, but Error with Internet Explorer and Jquery Ajax has a similar answer.
Sorry for the noise.

jquery ajax always throws error (never becomes success)

Below is my piece of code.
My issue is the function on success never gets called.
It always throws the error section.
$.ajax({
url: 'http://localhost/zd/get_list',
data: 'term=' + medical_provider,
dataType: 'json',
type: 'GET',
success: function(data){
alert('success');
},
error: function(jqXHR, textStatus, errorThrown){
alert(jqXHR);
}
});
when i alert jqXHR is get [object Object] and when I alert textStatus I get "error" and when I alert errorThrown I get a blank error message.
I am using jquery 1.11.0.
I never get the success alert.
Your help is greatly appreciated.
Also the the data returned in json is properly validated. I did a test on jsonlint.
Your get_list server-side functionality is erroring.
Use chrome developer tools to breakpoint and inspect your jqXHR object in the error callback, it will have more details about what is going wrong.
Edit:- based on your updated comments, try adding:
contentType: 'application/json',
to your ajax call parameters
So it turns out, the base url (was using codeigniter) was my computers ip address instead of localhost. Accessing the through the ip addy OR changing the base url to localhost worked.
Thank you all for the help.

Download JSON file via JavaScript/JQuery - HTTP-Status Ok - JQuery Error

I've got the following problem: I need to download a JSON file from an API via JQuery / JavaScript. In theory this should be quite basic.
I tried $.ajax and all of its siblings like $.get or $.getJSON. I alway get an 200 OK but my Firebug reports an error. Printing the error just says: "error" - so not that helful.
I read that maybe the JSON file is corrupt. So I tried it with a plain text file (*.txt). Same result.
The JSON file is valid, I check it against a validator.
I also tried ContentType and dateType and experimented with json and jsonp...
I basically used something like this (with a million variations for testing purposes):
$.ajax({
url: 'http://www.myurl.com/api/v1/myfile.json',
...
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error.statusText);
}
});
Am I missing something important here? It's really odd that nothing seems to change the behavior of the AJAX-call.
In fact I don't really need AJAX because I need to grab the JSON file when loading the page...
And the JSON file is not on the same domain as the AJAX caller.
Is that URL located on the same server you're trying to get the data from?
If not, you ran into a cross-domain request, which can only be handled using JSONP. So, the JSON file itself must be compatible with JSONP format, otherwise jQuery won't be able to process it (even if you provide a 'jsonp' dataType).
P.S.: Firebug will always show response code 200 but give an empty response body for such requests
Try in this way by disabling security
$.ajax( {
type : 'GET',
contentType : "application/json; charset=utf-8",
url : surl, \\specify your url
async : false,
dataType : 'json',
headers : {
Accept : "application/json",
"Access-Control-Allow-Origin" : "*"
},
crossDomain : true,
success : SucceedFunc,
error : function(data, textStatus, errorThrown) {
console.log("error" + ' ' + JSON.stringify(data) + ' ' + textStatus + ' ' + errorThrown);
}
});
function SucceedFunc(data) {
alert("success");
}
}
Did you try to catch the error the correct way?
$.ajax({
url: 'http://www.myurl.com/api/v1/myfile.json',
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error.message);
}
});
If you are using chrome go to cmd prompt and run the chrome by disabling the security. You can disable security using pathwhere_chrome_is_located\chrome.exe --disable-web-security
and run the html page. I think this may help you.

jQuery Ajax fails on Android tablet browser

I am developing on a Samsung tablet which has default browser.
The ISS tells me this info : Mozilla/5.0+(Linux;+U;+Android+3.2;+en-us;+GT-P7500+Build/HTJ85B)
The ajax call keeps failing with Error 0 and no information when posting info to server. On every other browser it works without problems.
Any idea why ?
here is the code i use :
status is 0
end textStatus is undefined
$.ajax({
url: uri,
contentType: "application/json;charset=utf-8",
dataType: "text",
data: input,
type: 'POST',
error: function (jqXHR, textStatus, errorThrown) {
doShowError("Error:" + jqXHR.status + "--" + textStatus + "--" + errorThrown + "-"+ jqXHR.readyState);
}
}
Edit 1 : Mozilla/5.0+(Linux;+U;+Android+3.2;+en-us;+GT-P7500+Build/HTJ85B)+AppleWebKit/534.13+(KHTML,+like+Gecko)+Version/4.0+Safari/534.13 this is all I get.
Found a solution :
It seems that when doing things asynchronously in this Webkit browser, when receiving multiple chunks of data from a large response, the browser responds with status 0 which means error, instead of 206 which means partial result. The advice for my case is that we should used sync in order to fix the issue until this bug is fixed : http://code.google.com/p/android/issues/detail?id=14924 or this one http://bugs.jquery.com/ticket/8343

Categories

Resources