Invisible Ajax errors - javascript

I'm developing a website and a recurrent problem makes me lose a lot of time.
Sometimes, when using Ajax to get informations from the server, I get errors, reported by the following Javascript code:
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(
"Error in ajaxRequest\n"
+ "Status : " + textStatus
+ "\nError: " + errorThrown
);
}
The alert gives something like
Error in ajaxRequest
Status : parsererror
Error: SyntaxError: JSON.parse: unexpected character at line 1 column 3 of the JSON data
But nothing seems wrong when I write in a file the data (just before it is sent back to client).
And Apache's error.log reports nothing.
When this occurs, I usually spend a lot of time commenting/uncommenting some code to see when the error occurs, but it's not practical at all (and very slow).
Is there a way to track those errors ? I didn't find on internet.
KR
Zlotz

Related

Connection faliure to the server in jQuery Ajax error

I'm trying to identify an error during submitting Ajax request via jQuery when the connection to the server is failed. I load the page from the server, then disconnect the network connection. The following error handle in the Ajax jquery is used:
error: function(xhr, status, error){
var errorMessage = xhr.status + ': ' + xhr.statusText
alert('Error - ' + errorMessage);
alert('Connection Error\nCould not connect to the server.');
$("#loading").removeClass("show");
$("#submit_btn").removeAttr("disabled");
}
The first alert prints out:
Error - 0: error
I could not able to evaluate the returned errors values to identify the error cause is due to connection failure or something else!
The docs say that an error code of 0 basically means the request could not be completed, while other errors could be due to request completing but some other error in the endpoint or with response.
So usually with the error code 0 you can assume that there was a network issue.

Uncaught SyntaxError: Invalid or unexpected token when using $(element).html to include jsp

I am brand new to JAVA (JSP and JSTL)
I make an Ajax call to an api endpoint and in the callback I want to load a jsp file, which contains jsp. So, I do something like
AjaxQueueManager.get("/xyz/abc/" + efg,
function(data) {
if (data.success) {
$(element).html('<jsp:include page="includes/jsp-file.jsp" />');
} else {
getResourcesFailureCallback(element, data.errors, "ocon-wrong", "Something's Wrong!");
}
},
function(error) {
console.log("#####", error.status, error.statusText);
}
);
If my jsp-file.jsp doesn't contain any javascript all works well for example, if I put
<p>Hello World</p>
it works fine, but as soon as I put javascript in the jsp file even the most basic javascript like
console.log("hello world");
I get an error
Uncaught SyntaxError: Invalid or unexpected token
$(element).html("<script type="text/javascript">
I understand the error, its cause of " getting mixed with the " of type="text/javascript" but I dont know how to fix it.

Chrome extension - unable to catch javascript error

I am trying to catch all JavaScript errors from a specific website via a chrome extension. I am very inexperienced with javascript and for some odd reason my error handler isnt catching all of the errors, most importantly, the one on line 191 "Load timeout for modules":
https://hastebin.com/atuwiboqec.js
The error "script error" on line 507 comes through just fine.
This is my content.js file that I have under "content_scripts" in the manifest, it runs at document_start:
var script=document.createElement("script");
script.src=chrome.runtime.getURL("myscript.js");
script.async=false;
document.documentElement.appendChild(script);
This is my myscript.js file:
window.addEventListener("error", handleException, false);
window.addEventListener("unhandledrejection", handleException, false);
function handleException(I_sMsg) {
console.log("Error0 occured: " + I_sMsg.message);
return cancelEvent(I_sMsg);
};
window.onerror = function ErrorHandler(errorMsg, url, lineNumber) {
console.log("Error1 occured: " + errorMsg);
return false;
};
window.addEventListener("timeout", function(e) {
console.log("Error2 occured: " + e.error.message);
return false;
});
When the error "script error" occurs, both Error0 and Error1 messages get printed. When the error "load timeout for modules" occurs, nothing happens even though the error shows up as "Uncaught Error: Load timeout for modules" in the chrome console, in red, with log level "error". This indicates that the error is being thrown:
error in console image
How can I catch that error, or even better, all errors from that site?
P.S I am unsure of how to trigger that load timeout error. It only happens every now and then. I believe a slow network is the cause.
EDIT: Updated the url domain to a backup one as it wasnt resolving.

Parse save method gives 400 error

The following code doesn't save anything to the database:
var UserObject = Parse.Object.extend("User");
var objectid = object.id;
var secondQuery = new Parse.Query(UserObject);
secondQuery.get(objectid, {
success: function(userObject) {
alert(userObject.get("fbId"));
userObject.set("provider_access_token", access_token);
userObject.set("provider_refresh_token", refresh_token);
userObject.set("provider_token_expire", expires_in);
userObject.save(null, {
error: function(error){
alert(error.message + error.code);
}
});
},
error: function(object, error) {
alert("Error:" + error.code + " " + error.message);
}
});
I'm not sure why, but it does give me an 400 HTTP error. What am I doing wrong?
I've checked that all my variables are set and correct (the alert works just fine).
You have a typo: errror > error
Fixing this will give you the real error of why isn't getting saved
Thanks to Juan Guarcia who pointed out that I had an type which led to the error not showing up. After fixing that my problem was easily solved.
The error I was getting was the following:
Parse::UserCannotBeAlteredWithoutSessionError206
Which means that user objects cannot be altered without using the master key of my application. However, this isn't supported inside the Parse Javascript SDK, only in the Cloud Code.
So I need to move the function to the Cloud Code and then save from there. Problem solved!

Yummly API " Uncaught SyntaxError: Unexpected token : "

I am trying to request data from the Yummly API with the following call
$http.jsonp('http://api.yummly.com/v1/api/recipes?_app_id='
+ $scope.apiId
+ '&_app_key='
+ $scope.apiKey
+ '&allowedAllergy[]=396^Dairy-Free'
+ '/?callback=JSON_CALLBACK' ).success(function(data) {
console.log(data);
}).error(function(error) {
});
And I keep getting this error saying " Uncaught SyntaxError: Unexpected token : " and when I click it takes me to the response. It is also not logging the data in the console.
The sever is returning JSON - not JSONP. This in turns causes the exception when the JSON text is executed in the hosting <script> context.
Running the following code in the console will generate the same error because {..} is in a Statement context:
{"foo": "bar"}
On the other hand, a valid JSONP response should look like this (which is valid syntax because {..} is in an Expression context):
JSON_CALLBACK({"foo": "bar"})

Categories