【问题标题】:Krajee file input invalid json responseKrajee 文件输入无效的 json 响应
【发布时间】:2015-03-03 10:40:55
【问题描述】:

我尝试将 krajee 文件输入集成到我现有的表单中。 DEMO SITE

当我从计算机浏览文件并单击上传按钮(插件内置)时,我收到此错误:SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.

这个插件的作者告诉我,我必须在我的 php 文件中编写有效的 json 响应才能使其工作,但他没有时间帮助像我这样的个别案例。所以我从网站上阅读了文档,它有这部分:(你可以在上面的演示网站上找到它)

发送数据(从服务器)

您在uploadUrl 中设置的服务器方法必须将数据作为json 编码对象发送回。您必须发送的唯一键是错误,这将是上传的错误消息,并将帮助插件识别文件上传中的错误。例如,来自服务器的响应将发送为 {error: 'You are not allowed to upload such a file.'}。注意:插件会自动验证并显示ajax异常错误。

重要

您必须从服务器发送有效的 JSON 响应,否则上传过程将失败。即使您没有遇到任何错误,您也必须至少从您的服务器发送一个空的 JSON 对象 {}。

要捕获和显示验证错误,您的 JSON 响应数据必须包含错误键,其值将是要显示的错误 HTML 标记。这是按照上面提到的设置。

不幸的是,我无法理解它,因为我只是一个新的 php 学习者,这超出了我的范围。但是我在这里有我的 php 文件,希望一些专家可以帮助我将 json 响应添加到上面解释的文档中。非常感谢您!

这是我的 php 文件:

<?php
if(isset($_POST["submit"])){
require("../configs/dbconnect.php");
/*Form variable   */
$owner = mysql_real_escape_string($_POST["owner"]);
$title = mysql_real_escape_string($_POST["title"]);
$description = mysql_real_escape_string($_POST["description"]);
$city = mysql_real_escape_string($_POST["city"]);
$brand = mysql_real_escape_string($_POST["brand"]);
$marketprice = mysql_real_escape_string($_POST["marketprice"]);
$price = mysql_real_escape_string($_POST["price"]);
$phone = mysql_real_escape_string($_POST["phone"]);
/*** the upload directory ***/
$upload_dir= 'uploads';

/*** numver of files to upload ***/
$num_uploads = 5;

/*** maximum filesize allowed in bytes ***/
$max_file_size  = 5000000;

/*** the maximum filesize from php.ini ***/
$ini_max = str_replace('M', '', ini_get('upload_max_filesize'));
$upload_max = $ini_max * 1024;

/*** a message for users ***/
$msg = 'Please select files for uploading';

/*** an array to hold messages ***/
$messages = array();
$err=array();

/*** check if a file has been submitted ***/
if(isset($_FILES['file']['tmp_name']))
{
    /** loop through the array of files ***/
    for($i=0; $i < count($_FILES['file']['tmp_name']);$i++)
    {
        // check if there is a file in the array
        if(!is_uploaded_file($_FILES['file']['tmp_name'][$i]))
        {
            $messages[] = 'No file uploaded';
        }
        /*** check if the file is less then the max php.ini size ***/
        //elseif($_FILES['image']['size'][$i] > $upload_max)
        //{
        //    $messages[] = "File size exceeds $upload_max php.ini limit";
        //}
        // check the file is less than the maximum file size
        elseif($_FILES['file']['size'][$i] > $max_file_size)
        {
            $messages[] = "File size exceeds $max_file_size limit";
        }
        else
        {
            //$temp = explode(".", $_FILES["file"]["name"][$i]);
            //$extension = end($temp);
            //$name[$i] = sha1(microtime()) . "." . $extension;
            $name[$i]=$_FILES["file"]["name"][$i];
            // copy the file to the specified dir 
            if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$upload_dir.'/'.$name[$i]))
            {
                /*** give praise and thanks to the php gods ***/
                $messages[] = $name[$i].' uploaded';
                $image_path[$i]=$upload_dir.'/'.$name[$i];
            }
            else
            {
                /*** an error message ***/
                $messages[] = 'Uploading '.$name[$i].' Failed';
            }
        }
    }
}
$image_path_string=serialize($image_path);
$sql = "INSERT INTO memberpost(owner, title, description, city, brand, marketprice, price, phone, image) VALUES ('$owner', '$title','$description','$city','$brand','$marketprice','$price','$phone', '" . $image_path_string . "')";
$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
 if(sizeof($messages) != 0)
{
    foreach($messages as $err)
    {
        echo $err.'<br />';
    }
}
}
?>

【问题讨论】:

标签: php jquery ajax json file-upload


【解决方案1】:

你的echo 我想... 把你的错误放在任何变量上然后echo json_encode(variable name)。这就是发送 JSON 对象的方法。

【讨论】:

    猜你喜欢
    • 2015-09-01
    • 2015-06-20
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 2012-10-30
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多