Ok so I want to make a simple "web scraping" app;
I have this simple code:
$.ajax({ url: '',
success: function(data) {
var elements = $(data) //<--error here
}
});
<h1>the html does not matter</h1>
<script src=".1.0/jquery.min.js"></script>
<script src ='error.js'></script>
This works, I get the elements, however, I will be running this up to 100000 times and there is this error that pops up and makes debugging a pain.
GET file:///images/side-rail-slot-4-chosen-min.png net::ERR_FILE_NOT_FOUND
The error es about in the $(data) call.
I have tried:
$(data).error(function(e){/*do nothig*/})
$(data).on('error',function(e){/*do nothig*/})
And I have even gone into the jquery file to try and insert a try-catch ....somewhere. but the error persists.
Ok so I want to make a simple "web scraping" app;
I have this simple code:
$.ajax({ url: 'http://sentence.yourdictionary./example',
success: function(data) {
var elements = $(data) //<--error here
}
});
<h1>the html does not matter</h1>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src ='error.js'></script>
This works, I get the elements, however, I will be running this up to 100000 times and there is this error that pops up and makes debugging a pain.
GET file:///images/side-rail-slot-4-chosen-min.png net::ERR_FILE_NOT_FOUND
The error es about in the $(data) call.
I have tried:
$(data).error(function(e){/*do nothig*/})
$(data).on('error',function(e){/*do nothig*/})
And I have even gone into the jquery file to try and insert a try-catch ....somewhere. but the error persists.
Share Improve this question edited Feb 26, 2018 at 19:23 Doopdon asked Feb 26, 2018 at 18:07 DoopdonDoopdon 1352 silver badges11 bronze badges 12- 1 add error handler to the ajax call.. – epascarello Commented Feb 26, 2018 at 18:08
- 1 U can also do try { } catch() in JS if needed. – Andrew Commented Feb 26, 2018 at 18:15
- 1 @Andrew try catch is not going to work with Asynchronous calls – epascarello Commented Feb 26, 2018 at 18:17
- I am sorry I didn't explain where the error was ing from. its not from the $.ajax is from the $(data). the I can't add a try catch to it because it is Asynchronous – Doopdon Commented Feb 26, 2018 at 18:34
-
Why can't you put
try
/catch
inside of your success callback?try { var elements = $(data); } catch(e) { /* do nothing */}
– mhodges Commented Feb 26, 2018 at 19:00
2 Answers
Reset to default 4you need to set an error handler, just like the success handler:
$.ajax({ url: 'http://sentence.yourdictionary./example',
success: function(data) {
console.log(data)
var elements = $(data)
},
error: function(err) {
console.log(err)
}
});
here is an implementation i found on jsfiddle:
https://jsfiddle/Sk8erPeter/AGpP5/
Edit (additions from the ments by @mhodges):
The error callback will only be called for HTTP status code of 400
or higher. Several error conditions can happen that will not be caught by the error.
The culprit seems to be
<div class="spanish_ad center">
<a href="http://spanish.yourdictionary./">
<img src="/images/side-rail-slot-4-chosen-min.png" width="300" height="250" nopin="nopin">
</a>
</div>
All other images are prefixed with http://cf.ydcdn/latest/
, so I tried prepending that to the URL and it worked just fine.
It may feel a bit hacky, but you can do a string replace on that text, like so:
var newData = data.replace('src="/images/', 'src="http://cf.ydcdn/latest/images/');
var elements = $(newData);