AJAX请求不起作用(POST)

时间:2022-10-13 15:14:26

I have a little problem with a POST request using AJAX. The data just doesn't seem to get stored in the database. When I used GET instead of POST it worked, but the problem is that it sends a huge amount of data since it is imagedata from a canvas on the page. I tried to print out the size of the data that is sent to make sure it actually is correct before it is sent (it was about 10500 characthers long).

我对使用AJAX的POST请求有一点疑问。数据似乎并不存储在数据库中。当我使用GET而不是POST时,它工作了,但是问题是它发送了大量的数据,因为它是来自页面上画布的imagedata。我尝试打印发送的数据的大小,以确保在发送之前它是正确的(大约10500个字符长)。

Here's the Javascript/AJAX-code:

这是Javascript / ajax代码:

if(type == "DrawingData" && Drawer)
{
    var CanvasData = canvas.toDataURL("image/png"); 
    var url = "DB_Com.php";
    var params = "DrawingData="+CanvasData;

    xmlhttp.open("POST", url, true);

    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");

    xmlhttp.send(params);
}

And here is the PHP-code:

这是php代码:

if(isset($_POST['DrawingData']))
{
    $Image = mysql_real_escape_string($_POST['DrawingData']);

    $query = "UPDATE BlackboardDrawing SET Data = '$Image'";
    mysql_query($query) or die(mysql_error());
}

Any help is is highly appreciated. Thanks!

非常感谢您的帮助。谢谢!

1 个解决方案

#1


0  

Your Ajax looks fine, but you need to POST the row id that you are updating. :

您的Ajax看起来不错,但是您需要发布正在更新的行id。:

if(type == "DrawingData" && Drawer)
{
    var CanvasData = canvas.toDataURL("image/png"); 
    var url = "DB_Com.php";
    var rowid = 'your_row_id';
    var params = "DrawingData="+CanvasData+"rowid="+rowid;

    xmlhttp.open("POST", url, true);

    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");

    xmlhttp.send(params);
}

And change your PHP script:

修改PHP脚本:

if(isset($_POST['DrawingData']))
{
    $Image = mysql_real_escape_string($_POST['DrawingData']);
    $rowid = (int)$_POST['rowid'];
    $query = "UPDATE BlackboardDrawing SET Data = '$Image' WHERE id='$rowid'";
    mysql_query($query) or die(mysql_error());
}

#1


0  

Your Ajax looks fine, but you need to POST the row id that you are updating. :

您的Ajax看起来不错,但是您需要发布正在更新的行id。:

if(type == "DrawingData" && Drawer)
{
    var CanvasData = canvas.toDataURL("image/png"); 
    var url = "DB_Com.php";
    var rowid = 'your_row_id';
    var params = "DrawingData="+CanvasData+"rowid="+rowid;

    xmlhttp.open("POST", url, true);

    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");

    xmlhttp.send(params);
}

And change your PHP script:

修改PHP脚本:

if(isset($_POST['DrawingData']))
{
    $Image = mysql_real_escape_string($_POST['DrawingData']);
    $rowid = (int)$_POST['rowid'];
    $query = "UPDATE BlackboardDrawing SET Data = '$Image' WHERE id='$rowid'";
    mysql_query($query) or die(mysql_error());
}