亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在laravel中使用fetch api刪除記錄?

在laravel中使用fetch api刪除記錄?

牛魔王的故事 2022-06-16 10:59:10
我正在嘗試使用 fetch api 刪除記錄 laravel,但出現以下錯誤:此路由不支持 GET 方法。支持的方法:刪除。路線:Route::delete('/persona_eliminar/{id}', 'PersonaController@eliminar');來自我獲取 id 的 foreach 的按鈕:<button class="btn btn-danger btn-sm"                        onclick="deletePersona({{$persona->id}})">                        <i class="fas fa-times">                        </i>                    </button>JS:function deletePersona(id) {fetch("/persona_eliminar/" + id, {    method: "DELETE",})    .then((res) => res.json())    .then((response) => {        console.log(response);    });}
查看完整描述

3 回答

?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

//use url with route

//install axios

//define axios

var axios = require('axios');

  axios({method: 'post',

        url: baseUrl + '/persona_eliminar',

        data: {_token: $('meta[name="csrf-token"]').attr('content'), id: id

}

    })

            .then(function (response) {

})


查看完整回答
反對 回復 2022-06-16
?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

我相信標準語法是


function deletePersona (id) {

  fetch('/persona_eliminar/' + id, {

    method: 'POST',

    body: JSON.stringify({

      _method: 'PUT'

    })

  }).then((res) => res.json())

    .then((response) => {

      console.log(response)

    })

}


查看完整回答
反對 回復 2022-06-16
?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

即使使用路由的兼容方法,我個人也遇到了 fetch API 的問題。所以我改用 axios,它預裝了默認的 Laravel 前端。


你可以這樣做:


axios.delete("/persona_eliminar/" + id)

.then((res) => {

  //

})

這是我測試它的方法


路線::


Route::delete('/bounties/{bounty}/delete', 'BountyController@destroy');

Axios 演示:

http://img1.sycdn.imooc.com//62aa9cb20001f5d107990202.jpg

查看完整回答
反對 回復 2022-06-16
  • 3 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號