简体   繁体   English

带有FormData的jQuery ajax POST不发送任何值

[英]JQuery ajax POST with FormData doesn't send any values

There seem to be lots of posts about this, but can't find a solution amongst them and I've hit a brick wall. 似乎有很多关于此的帖子,但其中找不到解决方案,我碰到了砖墙。 Help, dear Stackers! 亲爱的堆高车,救命!

I'm writing a bit of code that lets users choose a profile picture, crop it and then save the cropped image on the server. 我正在编写一些代码,使用户可以选择个人资料图片,对其进行裁剪,然后将裁剪后的图像保存在服务器上。 I'm using the Cropper.js library to handle the actual cropping and have established that's working because I can display the crop live with this code: 我正在使用Cropper.js库来处理实际的裁剪,并且已经建立了工作原理,因为我可以使用以下代码实时显示裁剪:

var imageData = cropper.getCroppedCanvas().toDataURL();
var profileimage = document.getElementById('myProfilePhoto');
profileimage.src = imageData;

Now I want to send the cropped image to the server, and the best method would appear to be create a blob and Ajax that over to a php script to handle it. 现在,我想将裁剪后的图像发送到服务器,最好的方法似乎是创建一个Blob和Ajax,然后将其放置到php脚本中进行处理。

cropper.getCroppedCanvas().toBlob(function (blob) {
    var formData = new FormData();
    formData.append('file', blob);
    formData.append('uuid', '{a unique identifier}');
    $.ajax({
         type:           'POST',
         url:            '{the php script}',
         data:           formData,
         processData:    false,
         contentType:    false,
         success: function (response) {
             console.log('Upload success: '+response);
         },
         error: function () {
             console.log('Upload error');
         }
    });
});

And at this stage I'm just getting my php script to var_dump($_REQUEST) so I can see in the console what the script thinks it's getting. 在这个阶段,我只是将我的PHP脚本添加到var_dump($ _ REQUEST),这样我就可以在控制台中看到脚本认为正在获取的内容。 However, the response is just sending back the default $_REQUEST objects like PHPSESSID and no file or uuid input. 但是,响应只是发送回默认的$ _REQUEST对象(如PHPSESSID),而没有文件或uuid输入。

What am I doing wrong? 我究竟做错了什么?

Thanks in advance for any pointers. 在此先感谢您提供任何指导。

If you are sending via post in order to see the request put this code 如果您通过邮寄发送以查看请求,请输入以下代码

 var formData = new FormData();
formData.append('file', blob);
formData.append('uuid', '{a unique identifier}');
formData.append('_method', 'PATCH'); <---add this and you're good to go

So the answer was annoyingly simple but frustratingly hard to find. 因此,答案很简单,但很难找到。

My server had an .htaccess rewrite rule which automatically changed www. 我的服务器有一个.htaccess重写规则,该规则会自动更改www。 addresses to the non-www version, and obviously I'd been dumb enough to include the www. 地址到非www版本,显然我已经足够愚蠢了以包括www。 in the URL I was calling, and it seems that's enough to strip off the $_POST data in these circumstances. 在我所调用的URL中,在这些情况下似乎足以剥离$ _POST数据。

So FormData() and blobs are all red herrings here - it's just a redirect that's the cause. 因此,FormData()和Blob在这里都是红色的鲱鱼-只是重定向而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM