如何使用带有数据的ajax重定向php页面?

    我知道这里有很多与我的标题相同的问题,但我无法使用为他们提供的答案来解决我的问题。

该程序应该在截取谷歌地图的屏幕截图后将页面重定向到OCR结果。发生的事情是,我只能通过网络>过程.php >预览来查看结果。

我也尝试在ajax上使用,但它只会导致页面出现很多错误,因为缺少数据。

以下是索引.phpwindow.location.href = "your-url-to-redirect-to";


<body>

<input type='button' class="btn btn-success" id='but_screenshot' value='Take screenshot' onclick='screenshot();'><br/>

<script type="text/javascript">

    function screenshot() {

        var transform = $(".gm-style>div:first>div:first>div:last>div").css("transform");

        var comp = transform.split(",")

        var mapleft = parseFloat(comp[4])

        var maptop = parseFloat(comp[5])

        $(".gm-style>div:first>div:first>div:last>div").css({ 

            "transform":"none",

            "left":mapleft,

            "top":maptop,

        });

        html2canvas(document.getElementById('map'), {

            useCORS: true

        }).

        then(function(canvas) {

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

                $.ajax({

                    url: 'processing.php',

                    type: 'post',

                    data: {image: base64URL},

                    success: function(data){

                        console.log('Upload successfully');

                    }

                });

            }

        );

    }

</script>

</body>

这是processing.php


<?php

// upload screenshot

$image = $_POST['image'];

$location = "uploads/";

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

// end


元芳怎么了
浏览 105回答 1
1回答

慕后森

&nbsp; &nbsp; 我知道这里有很多与我的标题相同的问题,但我无法使用为他们提供的答案来解决我的问题。该程序应该在截取谷歌地图的屏幕截图后将页面重定向到OCR结果。发生的事情是,我只能通过网络>过程.php >预览来查看结果。我也尝试在ajax上使用,但它只会导致页面出现很多错误,因为缺少数据。以下是索引.phpwindow.location.href = "your-url-to-redirect-to";<body><input type='button' class="btn btn-success" id='but_screenshot' value='Take screenshot' onclick='screenshot();'><br/><script type="text/javascript">&nbsp; &nbsp; function screenshot() {&nbsp; &nbsp; &nbsp; &nbsp; var transform = $(".gm-style>div:first>div:first>div:last>div").css("transform");&nbsp; &nbsp; &nbsp; &nbsp; var comp = transform.split(",")&nbsp; &nbsp; &nbsp; &nbsp; var mapleft = parseFloat(comp[4])&nbsp; &nbsp; &nbsp; &nbsp; var maptop = parseFloat(comp[5])&nbsp; &nbsp; &nbsp; &nbsp; $(".gm-style>div:first>div:first>div:last>div").css({&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "transform":"none",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "left":mapleft,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "top":maptop,&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; html2canvas(document.getElementById('map'), {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; useCORS: true&nbsp; &nbsp; &nbsp; &nbsp; }).&nbsp; &nbsp; &nbsp; &nbsp; then(function(canvas) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var base64URL = canvas.toDataURL('image/jpeg').replace('image/jpeg', 'image/octet-stream');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'processing.php',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'post',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {image: base64URL},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('Upload successfully');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }</script></body>这是processing.php<?php// upload screenshot$image = $_POST['image'];$location = "uploads/";$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);// enduploadToApi($file);function uploadToApi($target_file){&nbsp; &nbsp; require __DIR__ . '/vendor/autoload.php';&nbsp; &nbsp; $fileData = fopen($target_file, 'r');&nbsp; &nbsp; $client = new \GuzzleHttp\Client();&nbsp; &nbsp; try {&nbsp; &nbsp; $r = $client->request('POST', 'https://api.ocr.space/parse/image',[&nbsp; &nbsp; &nbsp; &nbsp; 'headers' => ['apiKey' => '[API_KEY]'],&nbsp; &nbsp; &nbsp; &nbsp; 'multipart' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'file',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'contents' => $fileData&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ], ['file' => $fileData]);&nbsp; &nbsp; $response =&nbsp; json_decode($r->getBody(),true);&nbsp; &nbsp; if(empty($response['ErrorMessage'])) {?><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; <title>Result</title>&nbsp; &nbsp; &nbsp; &nbsp; <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/css/bootstrap.min.css">&nbsp; &nbsp; &nbsp; &nbsp; <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>&nbsp; &nbsp; &nbsp; &nbsp; <script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/js/bootstrap.min.js'></script>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="exampleTextarea">Result</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <textarea class="form-control" id="exampleTextarea" rows="30">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($response['ParsedResults'] as $pareValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $pareValue['ParsedText'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?></textarea>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </body></html><?php&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; header('HTTP/1.0 400 Forbidden');&nbsp; &nbsp; &nbsp; &nbsp; echo $response['ErrorMessage'];&nbsp; &nbsp; }&nbsp; &nbsp; } catch(Exception $err) {&nbsp; &nbsp; &nbsp; &nbsp; header('HTTP/1.0 403 Forbidden');&nbsp; &nbsp; &nbsp; &nbsp; echo $err->getMessage();&nbsp; &nbsp; }}?>
打开App,查看更多内容
随时随地看视频慕课网APP