简体   繁体   English

CKEditor无法解析JSON响应

[英]CKEditor can not parse JSON response

What I have: 是)我有的:

  1. Symfony2 Symfony2的
  2. CKEditor with Image and Enhanced Image (also image2) addons 带有图像增强图像(也是图像2)插件的CKEditor

I found information about uploading files to server on official site : 我发现有关在官方网站上将文件上传到服务器的信息:

Example — Setting Up Image upload plugin: 示例 - 设置图像上载插件:

config.extraPlugins = 'uploadimage';
config.imageUploadUrl = '/uploader/upload.php?type=Images';

Response: File Uploaded Successfully When file is uploaded successfully then JSON response with the following entries is expected: 响应:文件成功上载文件上传成功后,需要使用以下条目的JSON响应:

  • uploaded – Set to 1. 已上传 - 设为1。
  • fileName – Name of uploaded file. fileName - 上传文件的名称。
  • url – URL to a uploaded file (URL-encoded). url - 上传文件的URL(URL编码)。

Example: 例:

{
    "uploaded": 1,
    "fileName": "foo.jpg",
    "url": "/files/foo.jpg"
}

Symfony returns JSON responce: Symfony返回JSON响应:

return new JsonResponse(
            array(
                'uploaded'  => '1',
                'fileName'  => $image->getName(),
                'url'       => $image->getWebPath()
            )
        );

After successfully uploaded an image I see: 成功上传图片后,我看到:

在此输入图像描述

And error in JS console: 和JS控制台中的错误:

Resource interpreted as Document but transferred with MIME type application/json: " http://example.com/app_dev.php/dashboard/settings/upload/image?CKEditor=example_post_content&CKEditorFuncNum=1&langCode=en ". 资源解释为Document但使用MIME类型application / json传输:“ http://example.com/app_dev.php/dashboard/settings/upload/image?CKEditor=example_post_content&CKEditorFuncNum=1&langCode=en ”。

But it must be working like on the official page (see second editor) 但它必须像在官方页面上一样工作(见第二编辑)

I tried to return other response from Symfony, like: 我试图从Symfony返回其他回复,例如:

$response = new Response();
        $response->headers->set('Content-Type', 'application/json');

        $response->setContent(
            json_encode(
            array(
                'uploaded'  => '1',
                'fileName'  => $image->getName(),
                'url'       => $image->getWebPath()
            )
        ));

        return $response;

but not works. 但不行。 Any idea? 任何的想法?

UPDATE UPDATE

I resolved the problem by using answer . 我通过回答解决了这个问题。 Final FCKeditor code look like: 最终的FCKeditor代码如下:

$response = new Response();

$response->headers->set('Content-Type', 'text/html');

$content = "<script type=\"text/javascript\">\n";
$content .= "window.parent.CKEDITOR.tools.callFunction(1, '".$image->getWebPath()."', '' );\n";
$content .= "</script>";

$response->setContent($content);

return $response;

Does anyone know another solution or why solution with JSON response doesn't work? 有没有人知道另一种解决方案或为什么JSON响应的解决方案不起作用?

只有在内容中粘贴图像时才使用JSON响应,对于从对话框中上传文件,您必须使用正常的javascript响应

What they have in their example in the second editor works exactly the same as you put in your UPDATE . 他们在第二个编辑器中的示例中的内容与您在UPDATE中的内容完全相同。

In response they have Content-Type: text/html and content is 作为回应,他们有Content-Type: text/html和内容

<script type="text/javascript">
  window.parent.CKEDITOR.tools.callFunction("92", "\/userfiles\/images\/side-nav.jpg", "");
</script>

So, there's unlikely to be another solution. 所以,不太可能有另一种解决方案。

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

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