简体   繁体   English

使用Ajax请求Rails控制器数据

[英]Use Ajax to request for rails controller data

I was hoping someone could help me out on this one, and a possible explanation. 我希望有人可以帮助我解决这个问题,并提供可能的解释。 I have a rails controller called get_songs that will get a hash of songs from the currently logged in user. 我有一个名为get_songs的rails控制器,它将从当前登录的用户获取歌曲的哈希值。 That said, whenever a particular button is clicked I want to get the data from the get_songs controller at put it in javascript variable. 也就是说,每当单击特定按钮时,我都希望从get_songs控制器获取数据,并将其放在javascript变量中。 The following is what I have so far. 以下是我到目前为止的内容。

playlist.js.erb playlist.js.erb

   function get_songs() {
 var mysongs;
 $.ajax({
    type : 'GET',
    url : "getSongs",
    dataType : 'json',
    success : function(response) {
    }
  });
}

My controlller 我的控制者

class StaticPagesController < ApplicationController


  respond_to :js, :json, :html

 def get_songs()
      if user_signed_in?
            session[:user_id] = current_user.id
            present_user = User.find(session[:user_id])
            present_user = present_user.playlist.keys
        @songs = present_user
    respond_to do |format|
  format.json  { render :json => @songs}

    end
    end

My routes: 我的路线:

  match '/getSongs', to: 'static_pages#get_songs', via: 'get'

I think problem in session. 我认为会议有问题。 When you send request using ajax, you don't send any cookies and session. 使用ajax发送请求时,您不会发送任何cookie和会话。 So try this 所以试试这个

$.ajax({
  type:"GET",
  beforeSend: function (request){
    //your token is here
    request.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr("content"));
  },
  url: "getSongs",
  dataType : 'json',
  success : function(response) {
    console.log(response)
  }
});

Also, make sure that you have <%= csrf_meta_tag %> in layouts/application.html.erb 另外,请确保您在layouts / application.html.erb中具有<%= csrf_meta_tag %>

UPD If you are using gem 'jquery-rails' and have //= require jquery_ujs in application.js file Content of csrf_meta_tag automatically will be added to all your ajax requests UPD如果您正在使用//= require jquery_ujs gem 'jquery-rails'并且//= require jquery_ujsapplication.js文件中//= require jquery_ujs ,则csrf_meta_tag内容将自动添加到您的所有ajax请求中

If everything was working you would do: 如果一切正常,您可以执行以下操作:

success : function(response) {
  mysongs = response;
}

The 'response' parameter on the success function is the result of the ajax call you are sending from your controller; 成功函数上的“响应”参数是您从控制器发送的ajax调用的结果。 the @songs json. @songs json。

A simple way to debug your javascript (to check what is the value of your 'response' after the ajax request) is to right-click on your browser and click on 'inspect', then go to the 'Sources' tab, look for your javascript file and click the line where you want to add the debug point (in this case the line with 'mysongs = response', then refresh the page. It should stop running at that point and show the value of 'response'. 调试javascript(检查ajax请求后“响应”的值是什么)的一种简单方法是右键单击浏览器,然后单击“检查”,然后转到“源”选项卡,查找您的javascript文件,然后单击要在其中添加调试点的行(在本例中为“ mysongs = response”的行,然后刷新页面。它应在该点停止运行并显示“ response”的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM