Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

SyntaxError: Unexpected token < in JSON at position 0 #155

Open
newscloud opened this issue Jan 31, 2019 · 4 comments
Open

SyntaxError: Unexpected token < in JSON at position 0 #155

newscloud opened this issue Jan 31, 2019 · 4 comments

Comments

@newscloud
Copy link

I'm also getting same error as earlier person. Reopening new issue.

SyntaxError: Unexpected token < in JSON at position 0

I've been working on this several hours. I agree with others. While we appreciate you offering a plugin, the examples are not sufficient.

@tonydspaniard
Copy link
Member

@newscloud can I see your code, implementation? thanks

@newscloud
Copy link
Author

newscloud commented Feb 6, 2019

public function actionCreate()
    {
        $model = new Photo();
        $model->id=1;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['create', 'id' => $model->id]);
        }
        return $this->render('create', [
            'model' => $model,
        ]);
    }


     public function actionUpload()
{
  $model = new Photo();
  $model->id=2;
  $model->image = UploadedFile::getInstance($model, 'image');
    if ($model->image !== null && $model->validate(['image'])) {
        Yii::$app->response->getHeaders()->set('Vary', 'Accept');
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $response = [];

        if ($model->save(false)) {
            // THIS IS THE RESPONSE UPLOADER REQUIRES!
            $response['files'][] = [
                'name' => $model->image->name,
                'type' => $model->image->type,
                'size' => $model->image->size,
                'url' => $model->getImageUrl(),
                'thumbnailUrl' => $model->getImageUrl(Photo::SMALL_IMAGE),
                'deleteUrl' => Url::to(['delete', 'id' => $model->id]),
                'deleteType' => 'POST'
            ];
        } else {
            $response[] = ['error' => Yii::t('app', 'Unable to save picture')];
        }
        @unlink($model->image->tempName);
    } else {
        if ($model->hasErrors(['model'])) {
            $response[] = ['error' => HtmlHelper::errors($model)];
        } else {
            throw new HttpException(500, Yii::t('app', 'Could not upload file.'));
        }
    }
    return $response;
}
<div class="photo-create">

    <h1><?= Html::encode($this->title) ?></h1>

    <?= FileUploadUI::widget([
        'model' => $model,
        'attribute' => 'user_id',
/* I think I also had tried photo/upload */
        'url' => ['photo/create', 'id' => $model->id],
        'gallery' => false,
        'fieldOptions' => [
            'accept' => 'image/*'
        ],
        'clientOptions' => [
            'maxFileSize' => 2000000
        ],
        // ...
        'clientEvents' => [
            'fileuploaddone' => 'function(e, data) {
                                    console.log(e);
                                    console.log(data);
                                }',
            'fileuploadfail' => 'function(e, data) {
                                    console.log(e);
                                    console.log(data);
                                }',
        ],
    ]); ?>
    <?php
    /* = $this->render('_form', [
        'model' => $model,
    ]) */?>

@elmys
Copy link

elmys commented Feb 13, 2019

@newscloud check for rightly line 'url' => ['photo/create', 'id' => $model->id],

@tonydspaniard
Copy link
Member

tonydspaniard commented Apr 6, 2019

@newscloud it looks ok as you use the same strategy I used on the upload action: https://github.com/2amigos/yii2-file-upload-widget/blob/master/src/actions/FileUploadAction.php#L104

This should be 'url' => ['photo/upload', 'id' => $model->id] as the create renders HTML and not JSON.

Other than the wrong action name, the rest looks fine

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants