我试图遵循本教程来实现sortablejs:
https://www.youtube.com/watch?v=r884jAqAbHY
我希望通过向服务器发送ajax更新来保持状态。本教程(Rails 6)是通过Rails.ajax完成的
在Rails 7中,这会导致控制台错误:
Uncaught ReferenceError: Rails is not defined 在Rails 7中执行异步请求的"Rails方式“是什么?
发布于 2022-03-10 09:52:54
取工作-我不知道这是否是“铁路的方式”。。。
fetch("URL", {
method: "PATCH",
headers: {
"X-CSRF-Token": document.querySelector("[name='csrf-token']").content,
},
body: data,
});发布于 2022-11-12 18:03:58
您可以使用https://github.com/rails/request.js#how-to-use
例如:
import { FetchRequest } from '@rails/request.js'
....
async myMethod () {
const request = new FetchRequest('post', 'localhost:3000/my_endpoint', { body: JSON.stringify({ name: 'Request.JS' }) })
const response = await request.perform()
if (response.ok) {
const body = await response.text
// Do whatever do you want with the response body
// You also are able to call `response.html` or `response.json`, be aware that if you call `response.json` and the response contentType isn't `application/json` there will be raised an error.
}
}https://stackoverflow.com/questions/71422105
复制相似问题