【问题标题】:Ajax jQuery POST variable not set未设置 Ajax jQuery POST 变量
【发布时间】: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”

标签: jquery ajax arrays post


【解决方案1】:

希望这对您有所帮助。这会将数组转换为 json,这是 php 可以解码并用作数组的东西。

var ImageJSON = JSON.stringify(Images);

  data: {
     Images: ImageJSON,
  }

在您发送之前。

if(isset($_POST['Images']))
    $Images = json_decode($_POST['Images']);

    foreach($Images as $image) {

【讨论】:

    【解决方案2】:

    我解决了:删除

       header('Content-Length: '.strlen($content)); 
    

    解决了问题(另见我的问题)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 1970-01-01
      • 2017-03-06
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多