dszm02606009 2016-10-30 07:09
浏览 99

Jquery AJAX POST到PHP不返回服务器上的数据

I'm using Fabric.js to create a basic desktop wallpaper editor. One of the features allows users to share their wallpapers.

On the click of this button, it converts the canvas to an image data url (data:image/png).

This all works fine, my issue is with the ajax post and it not submitting the data. It works perfectly fine on localhost, but when running on my online server, it doesn't submit the data.

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

For testing purposes, all my php file is doing is echoing the results:

$results = 'Image: ' . $_POST['image'] . ' Thumb: ' . $_POST['thumb'];
echo json_encode($results); exit ;

I have tried a combination of dataType being json and normal, along with contentType and processData being false. Once again, all these works locally, but when run on the server there is a blank response. I have also tried specifying the contestType as "application/json".

Without using the json dataType, on the server it just spits back the html page.

Any ideas/solutions to this problem? Could it be a server config or something? Cause all my other ajax I'm using for other functions work fine, it's just this one.

Edit: I have now added an error to the ajax:

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

With this error, it isn't triggered locally, but when on the server this error is triggered and the index html being returned. So the php file isn't even being triggered on the server.

I have something else which may be useful. On the server, when trying to access share.php, the page is getting a status of 302. But it will be fine and show up normally as 200 locally.

  • 写回答

2条回答 默认 最新

  • drsdvwsvo78320812 2016-10-30 07:38
    关注

    Maybe you can try

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

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条