jquery – 在rails应用程序中输出jSON

好的,rails 3这里的新开发者.

我希望我的jquery能够从rails 3应用程序获取项目的json对象.这是我的控制器

def yourprojects
  @projects = Projects.all(current_user)

  respond_to do |format|
    format.html # index.html.erb
    format.json  { render :json => @projects }
  end
end

添加了format.json行…在jquery我有

$.ajax({url: '/projects/yourprojects',dataType: 'json'});

所以应该工作我想.相反,服务器返回:“模板缺失”“缺少模板,与{:locale => [:en,:en],:handlers => [:rjs,:rhtml,:builder,:rxml,:erb],:formats => [:html]}在视图路径中“

你需要一个jsOn返回的模板吗? rails 3应用程序不应该知道如何格式化json?

路由文件

resources :projects do
    collection do
        get 'yourprojects'  
    end
end

解决方法

您可以将Accept:application / json标头设置为真实的REST,或者您可以将该格式添加到URL以进行快速骇客:
$.ajax({url: '/projects/yourprojects.json',dataType: 'json'});

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...