【问题标题】:Jquery AJAX POST to PHP not returning data on serverJquery AJAX POST到PHP未在服务器上返回数据
【发布时间】:2016-10-30 07:09:51
【问题描述】:

我正在使用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 ;

我尝试了将 dataType 为 json 和 normal 的组合,以及 contentType 和 processData 为 false 的组合。再一次,所有这些都在本地工作,但是在服务器上运行时会出现空白响应。我也尝试将竞赛类型指定为“application/json”。

不使用 json 数据类型,在服务器上它只是返回 html 页面。

对这个问题有什么想法/解决方案吗?可能是服务器配置或其他什么?因为我用于其他功能的所有其他 ajax 都可以正常工作,就是这个。

编辑: 我现在在 ajax 中添加了一个错误:

error: function(xhr, status, error) {
    console.log(xhr.responseText);
}

出现此错误时,不会在本地触发,但在服务器上会触发此错误并返回索引 html。所以php文件甚至没有在服务器上被触发。

我还有一些可能有用的东西。在服务器上,当尝试访问share.php时,页面的状态是302。但是没问题,在本地正常显示为200。

【问题讨论】:

  • 您是否尝试过这里的完整 url 路径 url: '/ajax/share.php' ?我的意思是像url: 'http://www.yoursite.com/ajax/share.php', 还要检查浏览器控制台上的响应代码。
  • @Mahfuzul Alam 控制台显示空白响应。尝试使用完整的网址和完全相同的问题。如果没有将processData设置为false,ajax根本不会将数据发布到php页面,而只是返回索引html代码。但是将 processData 设置为 false,数据不会被返回,因为它会成为 php 中的未定义索引。

标签: php jquery ajax server localhost


【解决方案1】:

也许你可以试试

$("#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);
            }
        });
    }
});

【讨论】:

  • 是的,这可以在本地工作,但是一旦我放入服务器,所有返回的都是索引 html 页面。就像它甚至没有被提交给 php.ini 一样。我试过http://www.yoursite.com/ajax/share.php,也没有任何改变。
【解决方案2】:

自己解决了。原来服务器不喜欢 data:image/png;base64,所以无论如何我需要通过 php 删除它,所以我只是先通过 JS 删除它,然后它会正常提交。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多