![](/img/trans.png)
[英]first invoke server side code then client side script without using AJAX
[英]Using jQuery AJAX to Invoke Rails Server Method on Client Side
我正在使用Ruby on Rails,並且想調用一個控制器方法作為我現有鏈接的onclick
事件。
我正在使用jQuery和AJAX做同樣的事情。 但是我對AJAX知之甚少。
這是我的代碼。
<script type="text/javascript">
$("a.browse_history").live("click", function()
{
alert("i am here");
$.ajax({
url: "/user_profile_controller.rb/save_user_history",
type: 'PUT'
});
});
</script>
在user_profile_controller.rb
:
def save_user_history
$doc_list << "hello hi"
end
我的鏈接是:
<a href="http://localhost:3000/documents" id="browse_history" class="browse_history">Click here</a>
但這是行不通的。任何解決方案將不勝感激。
您的網址看起來不正確。 運行rake routes
以獲取應用程序中現有路由的列表。
應該是這樣的
url: "/user_profile/save_user_history"
要么
url: "/user_profiles/save_user_history"
謝謝您的答復Stefan。
我這樣更改了代碼:
<script type="text/javascript">
function u_profile(user,url,query)
{
$.ajax({
type: 'POST' ,
url: "http://localhost:3000/user_profiles",
data : {
user_profile : {
user_id : user ,
query_term : query,
visited_url: url
}
} ,
success: function(data){
alert(data);
}
});
};
</script>
我將其作為鏈接的onclick事件調用。
它完全滿足了我在數據庫中使用傳遞的數據創建對象的要求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.