weixin_33736649 2015-07-22 08:21 采纳率: 0%
浏览 27

JS函数的AJAX调用

I'm trying to call a javascript function from an html.erb file. The javascript function makes an Ajax call to a specified controller, sending an item id from the URL as a parameter.

I keep getting syntax and "can't find variable: callAjax" errors.

in the html.erb file

<%= image_tag('star.png', :style => 'margin-right: 10px', :onclick => "callAjax('/book/favorite/'+book.id);") %>  

in the js file

function callAjax(path) {
  console.log(path);
    $.ajax({
        url: 'localhost:3000'+path,
        data: path.substring(path.lastIndexOf('/') + 1),
        success:{
          //do something
        }
    });
}

in the book controller

  def favorite
    book=Book.find(params[:id])
    current_user.favorite_books << book
  end
  • 写回答

0条回答 默认 最新

      报告相同问题?

      悬赏问题

      • ¥100 r语言多元回归模型怎么看表达式
      • ¥15 PMM 9010/30P
      • ¥15 pom文件依赖管理,未找到依赖
      • ¥15 现在后端返回给我一堆下载地址,都是一个视频切片后的,如何在uniapp安卓环境下将这些分片的视频下周并且合并成原始视频并下载到本地。
      • ¥15 Unity导出2D项目运行时图片变成马赛克
      • ¥15 关于communitytoolkit.mvvm的生成器得到的代码看起来没有被使用的问题
      • ¥15 matlab中此类型的变量不支持使用点进行索引
      • ¥15 咨询第六届工业互联网数据创新大赛原始数据
      • ¥15 Pycharm无法自动补全,识别第三方库函数接收的参数!
      • ¥15 STM32U575 pwm和DMA输出的波形少一段