gpt4 book ai didi

javascript - 在javascript中从ruby Controller 调用函数

转载 作者:太空宇宙 更新时间:2023-11-03 17:34:51 24 4
gpt4 key购买 nike

我想在 controllers/static_pages_controller.rb 中执行写入文件的操作:

def fileopen
my_file = File.new("public/CHNAME1.txt","w")
my_file.write "\tfasf"
my_file.close

end

(当我在 helper 中定义它并在 View 中调用它时,它工作得很好。)

在 myview.html.erb 中,我想要一些东西,比如我该怎么做?我在 application.js 中尝试过

function readfile() {
alert('readfile work')
$.ajax({
alert('ajax work')
url: "/fileopen",
type: "POST",
##don't know what to to to call fileopen
}
});
}

路线.rb

match '/fileopen', to:'static_pages#fileopen', via: 'get'

似乎什么都没发生。只有第一个警报起作用

最佳答案

你有语法错误,试试这个:

function readfile() {
alert('readfile work');
$.ajax({
url: "/fileopen",
type: "POST",
success: function(){
alert('ajax work');
}
});
}

我不知道 ruby​​ 但路由是在“GET”请求上,而你正在发出“POST”请求:

match '/fileopen', to:'static_pages#fileopen', via: 'get' // should be via: 'post'?

关于javascript - 在javascript中从ruby Controller 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20160858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com