dongtiao0657 2019-03-21 12:53
浏览 54

在javascript中从ajax调用PHP

trying to save page capture using html2canvas the captue is done but the save is not i am getting "method not allowed error" if i comment everything except for the php call, i get "not found" error i am a newbie to all this, may be i am missing something

both files test.cfm and export.php are found in the same folder cfsanad

test.cfm

<!doctype html>
<html>
<head>
<script type="text/javascript" src="SCRIPTS/es6-promise.min.js"></script>
<script type="text/javascript" src="SCRIPTS/es6-promise.auto.min.js"> </script>
<script type="text/javascript" src="SCRIPTS/html2canvas.js"></script>
<script type="text/javascript" src="SCRIPTS/jquery-2.1.0.min.js"></script>
</head>
<body>
<h1>Take screenshot of webpage with html2canvas</h1>
<div class="container" id='container' >
 hi
 bye
</div>
<input type='button' id='but_screenshot' value='save screenshot' onclick='exportAndSaveCanvas();'><br/>
<br/>
<!-- Script -->
<script type='text/javascript'>
function exportAndSaveCanvas()  {
try
{
   html2canvas(document.body).then(function(canvas) {

     document.body.appendChild(canvas);

     // Get base64URL
     var base64URL = canvas.toDataURL('image/jpeg').replace('image/jpeg', 'image/octet-stream');

     // AJAX request
     $.ajax({
       type: 'POST',
        url: 'export.php',
        data: {image: base64URL},
        success: function(data){
           alert('Upload successfully');
           },
        error: function(XMLHttpRequest, textStatus, errorThrown) { 
                alert("Status: " + textStatus+ "
Error: " + errorThrown);
        }
     }); //ajax

   }); //html2canvas
 }
 catch(err)
{
    alert('DBconnect: '+err.message);
    return 'err';
}
} // End exportAndSaveCanvas()
</script>

</body>
</html>

export.php

<?php
$message = "in php";
echo "<script type='text/javascript'>alert('$message');</script>";

$image = $_POST['image'];

$location = "IMAGES/";

$image_parts = explode(";base64,", $image);

$image_base64 = base64_decode($image_parts[1]);

$filename = "screenshot_".uniqid().'.png';

$file = $location . $filename;
file_put_contents($file, $image_base64);
?>
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug