[英]Jquery AJAX POST to PHP not returning data on server
我正在使用Fabric.js创建一个基本的桌面墙纸编辑器。 功能之一允许用户共享他们的墙纸。
单击此按钮后,它将画布转换为图像数据URL(data:image / png)。
一切正常,我的问题是ajax发布,但未提交数据。 它在localhost上运行良好,但是在我的在线服务器上运行时,它不提交数据。
$("#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 ;
我尝试了dataType为json和normal的组合,以及contentType和processData为false的组合。 所有这些再次在本地工作,但是在服务器上运行时,将出现空白响应。 我也尝试过将CompetitionType指定为“ 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删除了它,然后提交了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.