【问题标题】:ajax call causes internal server error 500 rails controllerajax 调用导致内部服务器错误 500 rails 控制器
【发布时间】:2013-08-19 02:06:58
【问题描述】:

我在 rails mvc 上使用 ruby​​,我正在尝试使用 ajax 调用将一些数据发送回服务器端。我对如何在 mvc 中发出和处理 ajax 请求感到困惑。

---------------- 在我的 javascript 文件中------

$.ajax({
    type: 'POST',
    url: 'http://localhost:3000/locations',
    data: { lat: position.coords.latitude, lng: position.coords.longitude },
    contentType: 'application/json',
    dataType: 'json'

});

我正在使用 respond_to 块在我的控制器中捕获此请求:

---------------- 在我的家庭控制器中 ------------------

respond_to do |format|
    format.js { 
      render 'users/locations' 
    }
    format.html {} 
end

---------------- route.rb ------------------------

post 'users/locations'
get 'users/locations'
match "/locations", to: "users#locations"

我尝试以这种方式提取数据:

----------------在我的位置操作-----------------

dasdasdd // used as a breakpoint to test whether this part was executed. it wasn't.
current_user.update_attribute :Latitude, params[:lat]
current_user.update_attribute :Longitude, params[:lng]

在调试 Firefox 控制台时,我看到内部服务器错误 500。这可能解释了为什么从未执行位置操作。但是为什么会出现错误 500?任何人都可以对我可能错的地方有所了解吗?

【问题讨论】:

  • 您可以在app/development.log 以及有关异常的500 错误的响应正文中找到大量信息。我们只是猜测您的问题是什么而不知道 什么 抛出的异常是。

标签: ruby-on-rails ajax internal-server-error


【解决方案1】:

由于你在ajax调用中指定的dataType是json,试试

format.json { 
      render 'users/locations' 
    }

而不是控制器中的 format.js

【讨论】:

  • 非常感谢,但我已经解开了谜团。这是我使用的设计认证模块的问题。当该模块在位置操作中失败时,某个方法不起作用,这会导致 500 内部服务器错误。 ajax 帖子显然需要一个用于身份验证的 csrf 标头。 stackoverflow.com/questions/7393871/…
猜你喜欢
  • 2013-08-11
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 2014-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多