【发布时间】:2014-05-10 02:23:32
【问题描述】:
问题:
我像这样进行 ajax 调用:
$.ajax({
type: "POST",
url: "cms/php/UploadAjax.php",
data: {
Images: Images
},
dataType: "html",
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
Images的结构是这样的:
Array
{
[0]=> Array
{
[0]=>"test1.png"
[1]=>"someDataUrl1"
}
[1]=> Array
{
[0]=>"test2.png"
[1]=>"someDataUrl2"
}
}
在我的本地服务器 (Windows/EasyPHP) 上,我可以访问 $_POST['Images'] 就好了,就像
<?php
if(isset($_POST['Images']))
{
foreach($_POST['Images'] as $image)
{
////do something with $image[0], $image[1]
if (!preg_match('/data:([^;]*);base64,(.*)/', $image[0], $matches))
{
die("error");
}
// Decode the data
$content = base64_decode($matches[2]);
header('Content-Type: '.$matches[1]);
header('Content-Length: '.strlen($content)); ////Removing it resolved the issue
$imagename=utf8_decode($image[1]);
$path="../../galleryimages/".$imagename;
$mysqlpath="galleryimages/".$image[1];
file_put_contents($path,$content);
}
}
else
echo "something";
?>
问题是,这在我的在线服务器上不起作用。它给出了警告:
POST http://myurl.com/... net::ERR_CONTENT_LENGTH_MISMATCH
如果我再添加:
contentType:"html/text"
对于我的 ajax 调用,我没有收到任何错误,但来自成功处理程序的 console.log(data); 给了我“something”,它告诉我 $_POST['Images'] 未设置。加上contentType,它既不能离线也不能在线。
提前致谢。
【问题讨论】:
-
将dataType表单“html”改为“json”