我正在使用Fabric.js创建一个基本的桌面壁纸编辑器。其中一个功能允许用户分享他们的壁纸。
单击此按钮,它将画布转换为图像数据url (data:image/png)。
这一切都很好,我的问题是ajax后,它没有提交的数据。它在本地主机上运行得很好,但在我的在线服务器上运行时,它不提交数据。
$("#share").click(function() {
if (confirm('Are you sure you want to share your wallpaper? YES | NO')) {
// Left out the rest of the Fabric.js canvas stuff here as that works fine
var image = canvas.toDataURL('png'),
var thumb = canvas.toDataURL('png');
var formData = {'image':image, 'thumb':thumb};
$.ajax({
url: '/ajax/share.php',
type: 'post',
data: formData,
dataType: "json",
success: function(response) {
console.log(response);
}
});
}
});
为了测试的目的,我的php文件所做的只是回显结果:
$results = 'Image: ' . $_POST['image'] . ' Thumb: ' . $_POST['thumb'];
echo json_encode($results); exit ;
我已经尝试了数据类型的json和正常的组合,随着contentType和processData是假的。同样,所有这些都在本地工作,但在服务器上运行时,会出现空白响应。我也试过将contestType指定为"application/json"。
不使用json dataType,在服务器上它只是返回html页面。
对于这个问题有什么想法/解决方案吗?是服务器配置还是别的什么?因为我在其他函数中使用的ajax都很好,只有这个。编辑:我现在在ajax中添加了一个错误:
error: function(xhr, status, error) {
console.log(xhr.responseText);
}
此错误不会在本地触发,但在服务器上触发此错误并返回索引html。所以php文件甚至没有在服务器上被触发。
我有一些可能有用的东西。在服务器上,当试图访问share.php时,页面的状态为302。但它会很好,并在本地正常显示为200
也许你可以试试
$("#share").click(function() {
if (confirm('Are you sure you want to share your wallpaper? YES | NO')) {
// Left out the rest of the Fabric.js canvas stuff here as that works fine
var image = canvas.toDataURL('png'),
var thumb = canvas.toDataURL('png');
$.ajax({
url: '/ajax/share.php',
type: 'post',
data: {'image':image, 'thumb':thumb},
dataType: "json",
success: function(response) {
console.log(response);
}
});
}
});
自己解决。原来服务器不喜欢data:image/png;base64,所以我需要通过php删除它,所以我先通过JS删除它,然后它就能正常提交了