drby30217 2014-05-10 10:57
浏览 189

通过AJAX将canvas.toDataUrl()发送到php

I am trying to send a canvas.toDataUrl() to a php page via AJAX.

Here's is my try:

JavaScript code:

function showUser() {
str = "url="+canvas.toDataUrl();

     if (window.XMLHttpRequest) {
             // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
     } else { // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
    xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
   }
    xmlhttp.open("GET","file.php?"+str,true);
    xmlhttp.send();
}

php:

<?php
   $url = $_GET['url'];
   echo "$url";
?> 

The code above doesn't seem to be working, although I did the exact same thing but with a String value instead, like the following:

    function showUser() {
str = "url=12345";

     if (window.XMLHttpRequest) {
             // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
     } else { // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
    xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
   }
    xmlhttp.open("GET","file.php?"+str,true);
    xmlhttp.send();
}

This one works fine, but when I use canvas.toDataUrl() it doesn't work !? why?

Is there another way to send canvas.toDataUrl() to php?

Thanks

  • 写回答

2条回答

  • douzhuozhu9544 2014-05-10 11:11
    关注

    I reccommend trying the javascript encodeURIComponent() function like this:

    str = "url="+encodeURIComponent(canvas.toDataUrl());
    

    The thing is, that dataurl contains symbols like "/" so it might get falsely interpreted. Let me know if that works.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料