0

我遵循了本教程:http ://www.youtube.com/watch?v=qUjQrThw-ss

我使用 xampp 来测试它。

问题是窗口不会在那里弹出。

这是我的代码:

HTML:

<!doctype html>
<html>
<head>
    <title>Html5 Drawing App!</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body style='margin: 0'>
    <div id="toolbar">
        <div id="rad">
            Radius <span id="radval">10</span>
            <div id="decRad" class="radcontrol">-</div>
            <div id="incRad" class="radcontrol">+</div>
        </div>
        <div id="colors">
        </div>
        <div id="save">
        Save
        </div>
    </div>
    <canvas id="canvas" style="display: block;">
        Sorry, Your Browser Is Unsupported. Switch To Another Browser
    </canvas>
    <script src="main.js"></script>
    <script src="radius.js"></script>
    <script src="colors.js"></script>
    <script src="save.js"></script>
</body>
</html>

保存.js:

var saveButton = document.getElementById('save');

saveButton.addEventListener('click', saveImage);

function saveImage(){
    var data = canvas.toDataURL();

    var request = new XMLHttpRequest();

    request.onreadystatechange = function(){
        if(request.readyState == 4 && request.status = 200){
            //do our stuff
            var response = request.responseText;
            window.open(response, '_blank', 'location=0, menubar=0');
        }
    }

    request.open('POST', 'save.php', true);
    request.setRequestHeader('Content-type', 'application/x-ww-form-urlencoded');
    request.send('img='+data);


    //window.open(data, '_blank', 'location=0, menubar=0'); 
}

保存.php:

<?php

$data = $_POST_['img'];

$data = str_replace('data:image/png;base64', '', $data);
$data = str_replace(' ', '+', $data);

$img = base64_decode($data);

$path = 'images/' . uniqid() . '.png';
if(file_put_contents($path, $img)){
    print $path;
}else{
    header("HTTP/1.1 500 Internal Server Error");
}

?>

为什么不弹出来?使用 Google Chrome 开发人员工具,我收到以下错误:

Uncaught ReferenceError: Invalid left-hand side in assignment save.js:11
POST http://localhost/draw/save.php 500 (Internal Server Error) save.js:20
3
Uncaught ReferenceError: Invalid left-hand side in assignment

请帮我解决这个问题。谢谢

4

2 回答 2

0

您在此行中省略了相等的 sgn:

if(request.readyState == 4 && request.status = 200){
                                             ^   should be ==
于 2013-10-27T19:05:50.877 回答
0

尝试修复以下错误:

if(request.readyState == 4 && request.status = 200){
                                             Λ
                                             |___ ==

request.setRequestHeader('Content-type', 'application/x-ww-form-urlencoded');
                                                        Λ
                                                        |___ www

$data = $_POST_['img'];
           Λ
           |___ $_POST[...
于 2013-10-27T21:19:43.360 回答