javascript - How to catch $() error in jquery - Stack Overflow

Ok so I want to make a simple "web scraping" app;I have this simple code:$.ajax({ url: '

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
 |  Show 7 more ments

2 Answers 2

Reset to default 4

you 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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745138872a4613334.html

相关推荐

  • javascript - How to catch $() error in jquery - Stack Overflow

    Ok so I want to make a simple "web scraping" app;I have this simple code:$.ajax({ url: '

    6小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信