簡體   English   中英

CKEditor無法解析JSON響應

[英]CKEditor can not parse JSON response

是)我有的:

  1. Symfony2的
  2. 帶有圖像增強圖像(也是圖像2)插件的CKEditor

我發現有關在官方網站上將文件上傳到服務器的信息:

示例 - 設置圖像上載插件:

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

響應:文件成功上載文件上傳成功后,需要使用以下條目的JSON響應:

  • 已上傳 - 設為1。
  • fileName - 上傳文件的名稱。
  • url - 上傳文件的URL(URL編碼)。

例:

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

Symfony返回JSON響應:

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

成功上傳圖片后,我看到:

在此輸入圖像描述

和JS控制台中的錯誤:

資源解釋為Document但使用MIME類型application / json傳輸:“ http://example.com/app_dev.php/dashboard/settings/upload/image?CKEditor=example_post_content&CKEditorFuncNum=1&langCode=en ”。

但它必須像在官方頁面上一樣工作(見第二編輯)

我試圖從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;

但不行。 任何的想法?

UPDATE

我通過回答解決了這個問題。 最終的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;

有沒有人知道另一種解決方案或為什么JSON響應的解決方案不起作用?

只有在內容中粘貼圖像時才使用JSON響應,對於從對話框中上傳文件,您必須使用正常的javascript響應

他們在第二個編輯器中的示例中的內容與您在UPDATE中的內容完全相同。

作為回應,他們有Content-Type: text/html和內容

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

所以,不太可能有另一種解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM