jquery发送ajax

    技术2022-07-11  160

    jquery发送ajax

    It's usually best to repair broken image paths as soon as possible because they can damage a website's credibility. And even worse is having a user tell you about it. Using jQuery and PHP, you can have your page automatically notify you of broken images.

    通常最好尽快修复损坏的图像路径,因为它们可能会破坏网站的信誉。 更糟糕的是让用户告诉您有关信息。 使用jQuery和PHP,您可以让页面自动将损坏的图像通知您。

    View Demo 观看演示

    PHP (The PHP)

    if(isset($_POST['image'])) { $to = 'errors@yourdomain.com'; $from = 'automailer@yourdomain.com'; $subject = 'Broken Image'; $content = "The website is signaling a broken image!\n\nBroken Image Path: ".stripslashes($_POST['image'])."\n\nReferenced on Page: ".stripslashes($_POST['page']); $result = mail($to,$subject,$content,'From: '.$from."\r\n"); die($result); }

    I keep the email short and to the point; it contains the broken image's src attribute and the page it was requested by.

    我将电子邮件简短明了; 它包含损坏的图像的src属性和它所请求的页面。

    jQuery JavaScript (The jQuery JavaScript)

    $(document).ready(function() { $('img').error(function() { $.post('ajax-image-error-jquery.php', { image: $(this).attr('src'), page: window.location.href }, function() { //hide the image? }); }); });

    For every image, we listen for the error event. When a broken image is discovered, an AJAX request is sent to the above PHP script.

    对于每个图像,我们都监听错误事件。 当发现损坏的图像时,AJAX请求将发送到上述PHP脚本。

    View Demo 观看演示

    Of course, if the page experiences high traffic before you can fix the image path, you'll have quite a few emails. You may prefer to store the error in a database table and check that often.

    当然,如果在修复图像路径之前页面访问量很大,您将收到很多电子邮件。 您可能更喜欢将错误存储在数据库表中并经常检查。

    翻译自: https://davidwalsh.name/broken-images-jquery-ajax

    jquery发送ajax

    Processed: 0.012, SQL: 9