Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获得;SyntaxError:“;JSON.parse:JSON数据第1行第1列的数据意外结束;根据我的API请求_Php_Laravel_Vue.js - Fatal编程技术网

Php 获得;SyntaxError:“;JSON.parse:JSON数据第1行第1列的数据意外结束;根据我的API请求

Php 获得;SyntaxError:“;JSON.parse:JSON数据第1行第1列的数据意外结束;根据我的API请求,php,laravel,vue.js,Php,Laravel,Vue.js,基本上,我已经使用laravel 7和Vue.js制作了一个名为observations的“简单”API,我的GET请求正在工作,但我不能执行删除和发布请求,我不得不说我还在学习laravel,所以可能会有很多错误 这里没有什么评论:DELETE实际上是有效的(它会删除),只是不会像预期的那样重新加载所有的观察结果 这是我的vuecomponent: <template> <div> <div class="card card-header"

基本上,我已经使用laravel 7和Vue.js制作了一个名为observations的“简单”API,我的GET请求正在工作,但我不能执行删除和发布请求,我不得不说我还在学习laravel,所以可能会有很多错误

这里没有什么评论:DELETE实际上是有效的(它会删除),只是不会像预期的那样重新加载所有的观察结果

这是我的vuecomponent:

<template>
    <div>
        <div class="card card-header">
            <h2> observaciones </h2><small> para futuras donaciones </small>
        </div>

        <div class="card card-body" v-for="observation in observations" v-bind:key="observation.id">
            <h4> {{observation.name}}</h4>
            <p> {{observation.content}}</p>
            <button class="btn btn-danger btn-block" @click="deleteObservation(observation.id)"> Borrar </button>
        </div>
        <!-- FORMULARIO PARA AGREGAR OBSERVACION -->
        <div class="card card-footer">
            <form @submit.prevent="addObservation">
                <!-- <div class="form-group">
                    <input type="text" class="form-control" placeholder="nombre" v-model="observation.name">
                </div> -->
                <div class="form-group">
                    <textarea type="text" name="content" class="form-control" placeholder="algun texto" v-model="observation.content">

                    </textarea>
                </div>
                <button type="submit" class="btn btn-primary btn-block"> Agregar observacion </button>
            </form>
        </div>
    </div>
</template>

<script>
    export default{
        data(){
            return{
                observations:[],
                observation:{
                    id:'',
                    name:'',
                    content:''
                }
            }
        },

        created(){
            this.fetchObservations();
        },

        methods:{
            fetchObservations(){
                fetch('api/observations')
                .then(res=> res.json())
                .then(data=>{
                    this.observations= data;
                    console.log(data);
                });
            },

            deleteObservation(id){
                if(confirm('¿Estas Seguro?')){
                    fetch('api/observations/+id',{
                        method:'delete'
                    })
                    .then(res=> res.json()) // aca estaria el error
                    .then((data)=>{
                        fetchObservations();
                        console.log(data);
                    })
                    .catch(err=> console.log(err));
                }
            },

            addObservation(){

                fetch('api/observations',{
                    method: 'post',
                    body: JSON.stringify(this.observation),
                    headers: {
                        'content-type':'application/json'
                    }
                })
                .then(res=> res.json())
                .then(data=>{
                    this.observation.content=''
                    alert("agregado")
                })
                .catch(err=> console.log(err));
            }
        }
    }
</script>
观测模型

class Observation extends Model
{
    //
    protected $fillable = ['name', 'content'];
}
及路线:

<?php

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::get('observations', 'ObservationController@index');

Route::post('observations', 'ObservationController@store');

Route::delete('observations/{id}', 'ObservationController@destroy');

因为
返回nulldestroy
方法中,当您试图将其解析为json时,前端会抛出一个错误

试试这个

            deleteObservation(id){
                if(confirm('¿Estas Seguro?')){
                    fetch('api/observations/+id',{
                        method:'delete'
                    })
                    .then(()=>{
                        fetchObservations();
                    })
                    .catch(err=> console.log(err));
                }
            }

因为
返回nulldestroy
方法中,当您试图将其解析为json时,前端会抛出一个错误

试试这个

            deleteObservation(id){
                if(confirm('¿Estas Seguro?')){
                    fetch('api/observations/+id',{
                        method:'delete'
                    })
                    .then(()=>{
                        fetchObservations();
                    })
                    .catch(err=> console.log(err));
                }
            }

谢谢你,先生,它工作得很好,现在我要做的只是观察issue@MartinAguirreFittipaldi考虑接受我的回答吗?谢谢你,先生,它工作得很好,现在我只不过是一个附加的观察。issue@MartinAguirreFittipaldi考虑接受我的答案吗?