0
我試圖做一個簡單的應用程序使用AJAX請求加載數據在同一頁面。ruby/sinatra處理ajax請求,但沒有任何反應
這裏的.RB代碼:
get '/' do
erb :index
end
post '/ssearch' do
if request.xhr?
keyword = params[:key]
items = extract_cse_info(keyword)
erb :ssearch, :locals => { 'items' => items }
else
erb :index
end
end
這裏有由layout.erb
$(document).ready(function(){
$('.SR_title').hide();
$('#msg').hide();
$('.SR_box').hover(function(){
$(this).find('.SR_title').toggle().stop(true, true);
$(this).toggleClass('SR_box_hover');
});
$("#searchform").submit(function(){
var form = $("#searchform"),
term = form.find("input[name='key']").val(),
url = form.attr("action");
$.ajax({
type: "POST",
url: url,
data: term,
success: function(msg){
$('#msg').html(msg);
}
});
});
加載.js文件這裏有index.erb代碼
<div class="search">
<form method="POST" action="/ssearch" id="searchform">
<input id="search" type="search" name="key" autocomplete="on" placeholder="search qui..." />
<button class="submit" type="submit">Go!</button>
</form>
<div id="msg"></div>
</div>
和執行一些東西的ssearch.erb。
<% i = 1 %>
<% items["items"].each do |x| %>
<div class="SR_box">
<div class="SR_title">
<h5>Title</h5>
<a class="SR_box_fs fa fa-arrows-alt" href="#"></a>
</div>
<iframe class="iframe" src=<%= "#{x['link']}"%> sandbox="allow-same-origin, allow-top-navigation, allow-forms"></iframe>
</div>
<% end %>
當我/一切正常,但在執行搜索沒有任何反應,並且瀏覽器只顯示沒有ssearch.erb創建的框架形式。爲什麼? 只需提示告訴我,說的服務器錯誤消息:
http://localhost:4567 - >/SSEARCH NoMethodError - 未定義的方法 '各自' 的零:NilClass: ssearch.erb:2: '在singletonclass塊' `
你能接受你的答案嗎? – Felix 2014-09-04 19:06:00