使用ajax和post将base64string传递到php文件
在这里使用移动应用程序,我使用post方法将图像编码数据发送到php文件,并从php文件中获取图像url。这里的问题是,我在使用ajax发送字符串时没有得到正确的图像。当我手动放置图像数据时,我可以查看图像,但是当使用ajax调用发送图像数据时,无法查看图像使用ajax和post将base64string传递到php文件,php,jquery,ajax,Php,Jquery,Ajax,在这里使用移动应用程序,我使用post方法将图像编码数据发送到php文件,并从php文件中获取图像url。这里的问题是,我在使用ajax发送字符串时没有得到正确的图像。当我手动放置图像数据时,我可以查看图像,但是当使用ajax调用发送图像数据时,无法查看图像 <?php //$image = $_POST['uploadedfile'];//not working if an ajax call is made what is the issue here $image
<?php
//$image = $_POST['uploadedfile'];//not working if an ajax call is made what is the issue here
$image ="base64 string of an image here";//working if i place base 64 string here
$binary = base64_decode($image);
$fileName = time() . ".jpeg";
file_put_contents('images/' . $fileName, $binary);
if (file_exists('images/' . $fileName)) {
$myjson12 = "[";
$myjson12.='{';
$myjson12.='"Certificate":"http://domain/demo/images/'.$fileName.'"';
$myjson12.='}';
$myjson12.="]";
echo "$myjson12";
} else {
echo 'FAILURE';
}
?>
请显示ajax调用。另外,ajax请求是从php脚本所在的同一域发送的吗?在任何人能够真正回答这个问题之前,您需要在您的问题中添加1。发布数据的JavaScript,以及2。收到了
$\u POST['uploadedfile']
的打印文件。请注意:您永远不想自己构建JSON,例如,将有效的JSON字符串组合在一起。出错的可能性太大了。相反,使用数组,并在最后使用保证有效结果的JSON\u encode()
将其转换为JSON。您显示的ajax调用是否直接来自您的代码?因为url属性中有double.php
。“url太长”问题表明您正在执行一个GET
请求,而不是POST
请求。这是一个编辑错误。我们只有filename.php…从ajax获取的值和在php文件中回显的值都是相同的值,但图像没有正确显示
$.ajax({
type: "POST",
url: "www.domain.com/getdata.php",
data: { "uploadedfile": c.toDataURL("image/jpeg") },
// dataType: "json",
success: function (response) {
console.log(response + "Sri");
$("#loadImg").hide();
alert("Success");
},
error: function (data) {
$("#loadImg").hide();
alert("Connection Failed");
}
});