I'm trying to load json files on page load. Those json are located inside a folder and constitute the basis on which the application is then built (each json file represents a page). The problem is that I want the script to be able to handle a random number of json file.
For now, I have the following recursive function which do exactly what I need, it loads each page and when an error occurs, it launches the application initialization. The problem is that my browser displays a 404 error, which is to be expected obviously. I want to know if there is a way to catch the 404 error (which indicates that no more json files are to be loaded) but prevent the error to be displayed in the browser's console?
$(document).ready(function() {
let pageArray = [];
loadPages(1, pageArray);
function loadPages(nb, array) {
$.ajax({
url: 'configuration/pages/page' + nb + '.json',
dataType: 'json',
error: function() {
initialize(array);
},
success: function(data) {
array.push(data);
loadPages(nb + 1, array);
}
});
}
function initialize(pages) {
console.log(pages);
}
});
EDIT
I ended up handling the configuration files with python (django on the server). So python is actually looping through the directory before sending the pages to the client. This doesn't answer the question but rather offers an alternative which is probably the best practice.
In order to prevent error from showing, you'll have to surround the piece of code which encounters the error in try/catch block... Something like this:
function() {
try {
openPage();
} catch (error) {
// do nothing
}
}
but I think this is not possible to do in your case, since those types of errors are logged by the browser...
look at: Hide 401 console.error in chrome dev tools getting 401 on fetch() call
Related
I just started to work with parse.com CloudCode. I am really new to Parse development and I managed to deploy their code trough the command line tool.
there is a main.js file that contain this function:
Parse.Cloud.define("hello", function(request, response) {
response.success("Hello world!");
});
now, I want to test and call this function, from the examples they told to run this function:
Parse.initialize('hQZZmu2UGiwr2QTHXrPSjG3ywAkFZccKQu12fD1S', 'IfaJZksyu21fKdITE0W8z9O2SWHpiLKlgUolfuRh');
Parse.Cloud.run('hello', {}, {
success: function(result) {
window.alert('success');
},
error: function(error) {
window.alert('error');
}
});
so, I am trying to call it from html file inside a
<script> //call to function here </script>
but nothing is happening.
I also try to add :
Parse.initialize('parse_id', 'js_key');
and still nothing.
can you please help?
I'm assuming those are your correct application id and javascript parse keys. You can check these in your parse application's settings.
Make sure you are including parse.js before you call that script.
<script src="//www.parsecdn.com/js/parse-1.4.2.min.js"></script>
Finally you can check your parse application's logs in the dashboard to check if the function even got called on the backend.
Take a look at these links to help get started:
https://parse.com/apps/quickstart#parse_data/web/existing
https://parse.com/docs/js/guide
Occasionally, an ajax request to Flickr's api will fail. I'm not sure if I'm doing something wrong here - or if I'm just not handling things correctly - but the code works over 90% of the time. When it doesn't work, I get the following error message from Firefox's console:
TypeError: jQuery19109306644694293944_1362865216185 is not a function
(I am letting jquery generate the callback, which is why the callback is named like that.)
This is the code that sometimes fails:
function getAppropriateSize(photo){
console.log("In getAppropriateSize");
/** stuff. query is defined here **/
$.ajax({
url: 'http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&format=json&api_key='+flickrKey+'&photo_id='+query.id,
dataType:'jsonp',
jsonp:'jsoncallback',
timeout:3000,
success: function(sizes){
console.log("In success - getAppropriateSize");
/**determine the correct size**/
flickrURL = sizes.sizes.size[currVal].source;
},
error: function(xmlhttprequest,textstatus,msg){
console.log("In error - getAppropriateSize");
/* handle error*/
}
});
}
I've checked what's returned when this happens and JSLint says it's valid javascript. flickrURL also gets set to a valid URL. I'm pretty mystified about what's causing this error - any help would be appreciated.
Edit: I was messing around and this time getAppropriateSize just received two separate messages from flickr for one call. The first one was
({stat:"fail", code:1, message:"Photo not found"})
The second one was the a full response from the server that also produced the TypeError mentioned above. However, the second response found the photo and gave me the sizes.
I need to grab some data from a JSP page that does a select on a database and then put inside a div. I need to do this with ajax.
Here is my code:
$(function() {
teste();
});
function teste() {
var v1 = document.getElementById("selCodigo").value;
alert(v1);
$.ajax({
type : "GET",
data : "turma="+v1,
url : "busca-notas.jsp",
success : function(resposta){
alert("DEU CERTO");
},
error : function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
document.getElementById("notas").innerHTML = "ERRO";
}
});
}
I tested the variable v1 and the value that it receives necessary, and in my JSP page, I do this:
String turmaSelecionada = request.getParameter("turma");
the problem is that the ajax content that does not feed into the div need, beyond what the xhr.status presents thrownError and a 404 error not found
Can anyone help me?
Either, busca-notas.jsp does not exist, or it is on a different server or path as the HTML calling the Ajax request.
Example: If your HTML and JavaScript is here:
http://www.example.com/somepath/page.html
and your PHP code is here:
http://www.example.com/otherpath/busca-notas.jsp
then you'll Need to use url: "../otherpath/busca-notas.jps". There is an easy way to check: Open your HTML in the browser, remove the last bit of the path, and replace it with "busca-notas.jpg", and see what you're getting.
A 404 also means, your JSP code never gets executed.
This is saying the resource you are trying to do a GET to is not there. The path you are doing a GET to is probably incorrect. Can you tell the structure of your files (javascript/service files etc...). I would suggest using the browser developer tools or fiddler to debug what is going on.
Use F12 (windows) with browsers to get to the developer tools. Also the fiddler tool is great! http://www.fiddler2.com/fiddler2/
On a side note if you use console.log for debugging you will never go back to alerts :)
Now I use this code to save error log from user to database
window.onerror = function (msg, url, line) {
$.ajax({
type: "POST",
url: "call/savelog.php",
data: {
type : "bug\n" + version,
message : 'Error message: '+msg+'\nURL: '+url+'\nLine Number: '+line
}
});
return true;
};
when error occur on jquery or kineticjs function
if it was in javascript console I will got something like this
Uncaught TypeError: Cannot read property '_id' of undefined kinetic-v4.0.2.js:1386
Kinetic.Node.remove kinetic-v4.0.2.js:1386
Kinetic.Layer.remove kinetic-v4.0.2.js:3667
drawCountdown side_play.js:105
so I'll know error cause by my drawCountdown function in my javascript file side_play.js
but it appear like this in my error log
Error message: Uncaught TypeError: Cannot read property '_id' of undefined
URL: https://myurl.com/js/kinetic-v4.0.2.js
Line Number: 1386
so I don't know which function that cause the error.
how to track down to the deepest function that cause error and save error log to database
You seem to be using the window.onerror event, and that's good. I would not recommend inserting try catch everywhere in your code, I've talked to a lot of people who log JavaScript errors. And when you begin to insert try catch.
There is a high risk that miss some places
Normal development becomes harder because your try catches hide errors for developers, what you really want is try catch in production and no try catch in development
The people who I have spoken to who have inserted try catch on large projects, have really regretted it.
You could try my project Muscula, It will try to fetch the javaScript source file, and show you the line of code that caused the error. This would solve your problem I think.
Also I auto-insert try catch in places where it's possible within reason (not everywhere), the rest of the errors are caught with window.onerror.
To use Muscula you just install a script, like Google Analytics, and you are logging errors. I would love for you to try it at let me know how it works for you. Right now Muscula is in open beta, and free to use. Used on approx. 4.000.000 pageviews every day.
You can wrap your functions in try/catch statements and if an error occurs you can catch errors and then write them in file via server for example.
try {
yourFunctionCallHere();
} catch (err) {
var message = 'yourFunctionCallHere failed: ' + err.message;
//now you can do smth with this error message, for example send an ajax request to the server...
}
In CRM Dynamics I'm executing the following code. I've tried to put it inside a try-catch statement to catch the allegedly uncaught exception but it makes no difference. I'm guessing that it occurs inside CRM server, C# code or something like that.
var temp = OData.read({
urlOrRequest: "https://myurl/2011/OrganizationData.svc/crmk_CustomEntitySet",
success: function(dataSet) { alert("Yippi!"); },
error: function(errorMessage) { alert("Buuuu..."); },
handler: null,
httpClient: null,
metaData: null
});
Of course, I've tested to execute the https://myurl/2011/OrganizationData.svc/crmk_CustomEntitySet call and it resturns the data as supposed to, so apparently I've got connection and access right in order.
However, I get neither of the callbacks to execute. Everything is just ghostly quiet. As I attempt to close or reload the page, though, I get an error message from CRM Dynamics, the partial contents of which are listed below.
<ScriptErrorDetails>
<Message>Exception thrown and not caught</Message>
<Line>13</Line>
<URL>/%7B634854498230000000%7D/WebResources/crmk_MyResourceWithDataJS</URL>
<PageURL>/userdefined/edit.aspx
?etc=10008&pagemode=iframe&preloadcache=1349853066209</PageURL>
<Function></Function>
<CallStack></CallStack>
</ScriptErrorDetails>
Given my competence level with CRM Dynamics (low) and with DataJS package (loooow), I'm stuck having no clue as to why it doesn't work nor how to trouble-shoot it.
I've checked out every example I could find on their page
You're getting no callback execution because you're not sending in one. You think you do but you don't. You're using the JSON syntax putting the properties (or whatever they're called) in an object. You're intending the following list of inputs.
The URL to organization data.
A function telling what to do if successful.
A function telling what to do if failed.
A bunch of nulls.
But since you package it into a structure, the CRM sees the following (regarding the full signature of the function odata.read(...) as seen in the source file..
An object with some stuff in it, not constituting a parsable URL.
Nothing at all, meaning null, while CRM wants to see a function (success).
Nothing at all, meaning null, while CRM wants to see a function (fail).
Nothing at all, meaning null, while CRM wants to see a function (serialization).
Nothing at all, meaning null, while CRM wants to see a HTTP client layer (whatever that might be).
Nothing at all, meaning null, while CRM wants to see some metadata.
Then a crash occurs inside the CRM server (since you failed to provide a parsable URL to the organization data that you're trying to read) and an exception is being thrown. But you haven't provided a callback function for receiving the notification of such an error, which is the message you've mentioned.
Your try-catch doesn't catch anything since there's no exception to catch. It's already been caught inside CRM Dynamics server. What you see is that it complains that there's no function to be called for notifying you about that.
Try to execute an equivalent to this code and see if you're successful (or at least get another error message).
var temp = OData.read(
"https://myurl/2011/OrganizationData.svc/crmk_CustomEntitySet",
function(dataSet) { alert("Data retrieval successful."); },
function(errorMessage) { alert("Operation failure!"); }
});
I'm assuming that you're using an other URL than what you've shown us in your code sample. It's just an example, right? You can test pasting in the very URL into IE and see if you'll get a reply and what it contains.