Connection faliure to the server in jQuery Ajax error - javascript

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.

Related

Invisible Ajax errors

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

OpenTok JS websocket connection timeout issue

I'm using opentok client JS api OpenTok.js 2.14.6 753045900, to set up a video conferencing app. Intermittently, it throws errors into chrome console:
WebSocket connection to 'wss://mantis002-ams.tokbox.com/rumorwebsocketsv2?socketId=12796b11-4174-431e-980f-52c29c32aa0d&attempt=b58d3a4d-7721-4e93-b39b-76224b00864a' failed: WebSocket is closed before the connection is established.
WebSocket connection to 'wss://mantis002-ams.tokbox.com/rumorwebsocketsv2?socketId=12796b11-4174-431e-980f-52c29c32aa0d&attempt=0d49b33e-33b9-4eec-a6d4-80ea6d7908ec' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT
OT:RumorSocket:1 Error code unavailable.
OT.Raptor.Socket error: e {code: 4020, message: "Error code unavailable."}
OT_SOCKET_CLOSE_FALLBACK_CODE: Error code unavailable.
OT.exception :: title: undefined (4020) msg: Error code unavailable.
s {code: 4020, message: "Error code unavailable.", name: "OT_SOCKET_CLOSE_FALLBACK_CODE", stack: "Error: Error code unavailable.↵ at https://stat…tatic.opentok.com/v2/js/opentok.min.js:29:247034)"}
An error occurred in an event handler TypeError: Cannot read property 'id' of
null
at index.js:455
at P (RaptorSocket.js:75)
at r (RaptorSocket.js:265)
at d.triggerCallback (Dispatcher.js:32)
at n.<anonymous> (SessionDispatcher.js:179)
at n.emit (events.js:84)
at d.trigger (eventing.js:321)
at d.emit (eventing.js:330)
at RaptorSocket.js:279
How to correctly handle this situation, because seems like reconnection doesn't work inside OpenTok.js?
And when I tried to recreate session I received
OT.exception :: title: Unexpected Server Response (2001) msg: Unexpected server response. Try this operation again later.
{ code: 2001, message: "Unexpected server response. Try this operation again later.", name: "OT_UNEXPECTED_SERVER_RESPONSE", stack: "Error: Unexpected server response. Try this operat…/static.opentok.com/v2/js/opentok.min.js:23:8742)"}
mixin.js:23 OT:RumorSocket:49 The connection is being terminated because the endpoint has indicated that reconnections are not available. (CLOSE_UNSUPPORTED)
TokBox Developer Evangelist here.
Could you please email support#tokbox.com with the sessionId along with the logs? The support team will be able to help you discover the underlying issue. You can also use the Session Inspector tool for a high-level summary that may help you pinpoint the connection errors.

Reading the error value in Javascript

I am handling the error in a if block in the method where I receive the response from the server. I put the server down in order to test the service unavailable scenario
_onResponse: function (err, res) {
if (err){
}
on doing alert("error " + err);
I receive
Error: Request has been terminated
Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.
at Request.crossDomainError (http://localhost:9000/index.js:33875:14)
at XMLHttpRequest.xhr.onreadystatechange (http://localhost:9000/index.js:33945:20)
I want to read the value of Error:
I tried doing err.message but then I receive the entire body of the error.
I tried with err.name I just get the text 'Error'
How do I get the content Request has been terminated in a javascript variable?
From the response all I can say is this is not the error your server is sending. It is raised by the engine while doing XHR to a domain for which CORS is disabled. To catch this error, wrap the XHR code in index.js around line 33945 in a try{} catch(e){} block. Inside catch block you get hold of the error using the e variable.
Your block will handle error only when you get a response from the target server.

Backbone model.save calls error function on status code 200

this.model.save(newModel, {error: function (e){
alert("Error trying to save contact: " + e);
console.log(e);
}});
Above is the code that is running on the client side. the server code is
model.update_contact(contact, function(err){
if(err){
res.statusCode = 500;
return res.json({"Database error" : err});
}
return res.end();
});
inspecting the network traffic i see that the server responds with status code 200 but the web page shows the alert message "Error trying to save contact: [Object object]"
p.s the db query is successful
Backbone expects to get the model's JSON back in response to PUT or POST requests IIRC.
Instead of returning res.end() try this:
return res.json(contact);
Dumb mistake, my server was not properly returning the object back. Once i added code to return the object everything ran fine.

How to get original statusText with jQuery

I try to access the statusText on a post back to the server when an error occured. On the server side I'm setting the status text to an error message. StatusCode is 500 and statusText the error description.
Chrome console output inside the Headers section is:
Status Code:500 (big red dot) Error Message: Something went wrong.
But when I try to access the statusText now inside of the .error() function callback there is always the text "error":
.error(function (xhr, text) {
alert(xhr.statusText);
});
Is jQuery overwriting this field or how can I access the original values?
Thanks for help!
I think you need to see the 3rd argument to the callback, it should be the HTTP status text. (according to the jQuery API documentation for the error callback)
function (xhr, ts, err) {
// ts => "error", "abort", etc (jQuery-specific)
// err => HTTP error from server
}

Categories