douao1959 2017-05-01 06:26
浏览 293

Ajax锚点链接触发器不起作用

I am using html2canvas javascript to convert html div to image file. All script are working good and it converts html div to image properly.

But when I am adding response(anchor link) to href and triggering it using ajax it is not triggering anchor link.

Here is my code,

Html code:

<div id="mainDiv">

<h1>Heading H1</h1>
<p>Download Image using ajax and php</p>

</div>

<div>
<a id="download">Download Image</a><br/>
<a id="download-image" href="" download>Download</a>
</div>

Ajax code

<script>

    $('#download').on('click', function () {
html2canvas([document.getElementById('mainDiv')], {
    onrendered: function (canvas) {
        var imagedata = canvas.toDataURL('image/png');
        var imgdata = imagedata.replace(/^data:image\/(png|jpg);base64,/, "");
        //ajax call to save image inside folder
        $.ajax({
            url: 'save_image.php',
            data: {
                   imgdata:imgdata
                   },
            type: 'post',
            success: function (response) {   
               $('#download-image').attr('href', response).trigger('click');
               //$('#downlaod-image').trigger('click');
            }
        });
    }
});
});

</script>
  • 写回答

1条回答

  • dpd3447 2017-05-01 06:31
    关注

    Clicking the link will actually reload your page. It will still execute html2canvas(), but the response from the Ajax call will never be processed, as by that time the page has already been reloaded.

    So cancel the default hyperlink action by adding a return false just before the end of the click handler (after the call to html2canvas).

    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题