无法从Javascript调用php文件

时间:2013-07-16 05:22:17

标签: php javascript clipboard

我正试图从我的javascript调用php文件,但它不起作用。没有错误,但它没有按预期工作。以下是我的代码。

的Javascript

function getImage() {
    obj = document.getElementById('paste-image');
    postTo = "http://clipboard.netau.net/Web/shoot.php";

    image = obj.getClipboardImageURL(postTo);

    if (image) {
        url = "shots/" + image;

        document.getElementById("target").src = url;
        document.getElementById("url").value = document.getElementById("target").src; // to get full path, hack, I know ;)
        document.getElementById("container").style.display = "";
    }
}

shoot.php

<?php   
$filename = uniqid() . '.jpg';
$result = file_put_contents( "./shots/" . $filename, file_get_contents('php://input') );
if (!$result) {
    print "error";
    exit();
}   
echo $filename;
?>

我已将代码上传到免费托管中。的 clipboard.netau.net

1 个答案:

答案 0 :(得分:0)

您可以简单地使用AJAX调用 -

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://xyz.abc.com/abc.php",true);
xmlhttp.onreadystatechange = function () {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var file = xmlhttp.responseText;
    }
};
xmlhttp.send();

通过这个,你可以在变量'file'中获得完整的php文件。