最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CKEditor can not parse JSON response - Stack Overflow

programmeradmin2浏览0评论

What I have:

  1. Symfony2
  2. CKEditor with Image and Enhanced Image (also image2) addons

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:

  • uploaded – Set to 1.
  • fileName – Name of uploaded file.
  • url – URL to a uploaded file (URL-encoded).

Example:

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

Symfony returns JSON responce:

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

After successfully uploaded an image I see:

And error in JS console:

Resource interpreted as Document but transferred with MIME type application/json: ".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:

$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

I resolved the problem by using answer. Final FCKeditor code look like:

$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?

What I have:

  1. Symfony2
  2. CKEditor with Image and Enhanced Image (also image2) addons

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:

  • uploaded – Set to 1.
  • fileName – Name of uploaded file.
  • url – URL to a uploaded file (URL-encoded).

Example:

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

Symfony returns JSON responce:

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

After successfully uploaded an image I see:

And error in JS console:

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".

But it must be working like on the official page (see second editor)

I tried to return other response from Symfony, like:

$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

I resolved the problem by using answer. Final FCKeditor code look like:

$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?

Share Improve this question edited May 23, 2017 at 11:46 CommunityBot 11 silver badge asked Oct 18, 2015 at 10:49 Max LipskyMax Lipsky 1,8421 gold badge19 silver badges30 bronze badges 3
  • Can you show the js code that calls the backend? – Carlos Granados Commented Oct 18, 2015 at 12:38
  • why is there a dollar sign in $return new JsonResponse – chiliNUT Commented Oct 21, 2015 at 17:24
  • @chiliNUT just a copy-paste mistake – Max Lipsky Commented Oct 21, 2015 at 18:00
Add a comment  | 

2 Answers 2

Reset to default 6

The JSON response is used only when you paste an image in the content, for file uploads from the dialogs you must use the normal javascript response

What they have in their example in the second editor works exactly the same as you put in your UPDATE.

In response they have Content-Type: text/html and content is

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

So, there's unlikely to be another solution.

发布评论

评论列表(0)

  1. 暂无评论