7号轨道上的响应是*/*,而不是涡轮流



当点击刺激控制器时,它会到达rails刺激控制器,但请求是*/*,但我希望这个请求是turbo_stream,那么我该怎么做呢。app/views/stimulation/index.html.erb

<div data-controller="toggle" data-toggle-url-value="<%= root_path %>">
<div>
<p class="mt-5">Copy text content</p>
<input type="text" data-toggle-target="txt"/>
<button data-action="click->toggle#copy">Copy</button>
</div>

app/controllers/stimulus_controller.rb

def index

end

app/javascript/controllers/stimulus_controller.js

static targets = ["txt"]
static values = { url: String }
connect() {
this.hideBtnTarget.hidden = true
console.log(this.urlValue);
fetch(this.urlValue).then((success) => {
console.log(success);
}).catch((e) => {
console.log(e);
})
}
copy(){
let txt = this.txtTarget
txt.select()
document.execCommand("copy")
txt.value = ""
}

我希望响应为turbo_stream,而不是*/*

谢谢!

要将请求作为turbo_stream获取,需要在fetch方法头中添加代码。

fetch("your url path", {
method: "get",
headers: {
Accept: "text/vnd.turbo-stream.html"
}, 
});

最新更新