无法在Rails中访问有关AJAX响应的数据

时间:2017-04-25 17:16:18

标签: javascript ruby-on-rails ajax

我在这里跑圈子 - 而且我有迹象表明我哪里出错了,但是我无法做到正确,所以希望能在这里得到答案 -

我从放在视图中的js函数发出以下ajax请求:

查看:

    $.ajax({
      type: 'POST',
      url: '/retrieve-location/',
      data: {coordinates: { lat: pos.lat, lng: pos.lng}},
      dataType: "html",
      success: function(data,status,xhr){
        console.log('There you go: ' + data);
        console.log('Success');
      },
      error: function(xhr,status,error){
        console.log(xhr);
        console.log('Somethings gone wrong');
      }
    });

路线:

match 'retrieve-location/', to: 'after_signup#ajax_geolocation', via: 'post'

after_signup控制器:

  def ajax_geolocation

    ...
    respond_to do |format|
      format.json  { render json: @coordinates, :status => :ok }
    end

  end

所以一切正常 - 我将DataType从JSON更改为HTML的原因是因为使用JSON它只是在ERROR函数中结束,即使头部设置为OK。

问题

console.log('There you go: ' + data);

输出==>

There you go: Turbolinks.clearCache()
Turbolinks.visit("http://localhost:3000/en", {"action":"replace"})

但我希望坐标。这只是一个测试电话 - 但我想建立在它上面,我不知道为什么我得到Turbolinks.clearCache()?

所以我唯一的预感是this - 但我没有使用表格,所以在这里有点迷失。

更新

将数据类型更改为json后,我最终出现在响应的ERROR端,并显示以下错误消息:

SyntaxError: Unexpected token T in JSON at position 0
    at JSON.parse (<anonymous>)
    at jQuery.parseJSON (jquery.self-bd7ddd3….js?body=1:9012)
    at ajaxConvert (jquery.self-bd7ddd3….js?body=1:9336)
    at done (jquery.self-bd7ddd3….js?body=1:9790)
    at XMLHttpRequest.callback (jquery.self-bd7ddd3….js?body=1:10312)

更新 - 增加了邮递员追踪

在POST后输入:http://localhost:3000/retrieve-location.json?coordinates%5Blat%5D=48.144859&coordinates%5Blng%5D=16.297634

BODY

ActionController::InvalidAuthenticityToken at /retrieve-location.json
=====================================================================

> ActionController::InvalidAuthenticityToken

actionpack (5.0.1) lib/action_controller/metal/request_forgery_protection.rb, line 195
--------------------------------------------------------------------------------------

``` ruby
  190           def initialize(controller)
  191             @controller = controller
  192           end
  193   
  194           def handle_unverified_request
> 195             raise ActionController::InvalidAuthenticityToken
  196           end
  197         end
  198       end
  199   
  200       protected
```

App backtrace
-------------



Full backtrace
--------------

 - actionpack (5.0.1) lib/action_controller/metal/request_forgery_protection.rb:195:in `handle_unverified_request'
 - actionpack (5.0.1) lib/action_controller/metal/request_forgery_protection.rb:223:in `handle_unverified_request'
 - devise (4.2.0) lib/devise/controllers/helpers.rb:253:in `handle_unverified_request'
 - actionpack (5.0.1) lib/action_controller/metal/request_forgery_protection.rb:218:in `verify_authenticity_token'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:382:in `block in make_lambda'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:169:in `block (2 levels) in halting'
 - actionpack (5.0.1) lib/abstract_controller/callbacks.rb:12:in `block (2 levels) in <module:Callbacks>'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:170:in `block in halting'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:454:in `block in call'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:454:in `call'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:448:in `block (2 levels) in around'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:286:in `block (2 levels) in halting'
 - route_translator (5.0.5) lib/route_translator/extensions/action_controller.rb:27:in `set_locale_from_url'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:382:in `block in make_lambda'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:285:in `block in halting'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:447:in `block in around'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:455:in `call'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
 - actionpack (5.0.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
 - actionpack (5.0.1) lib/action_controller/metal/rescue.rb:20:in `process_action'
 - actionpack (5.0.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
 - activesupport (5.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
 - activesupport (5.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
 - activesupport (5.0.1) lib/active_support/notifications.rb:164:in `instrument'
 - actionpack (5.0.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
 - actionpack (5.0.1) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
 - activerecord (5.0.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
 - actionpack (5.0.1) lib/abstract_controller/base.rb:126:in `process'
 - actionview (5.0.1) lib/action_view/rendering.rb:30:in `process'
 - actionpack (5.0.1) lib/action_controller/metal.rb:190:in `dispatch'
 - actionpack (5.0.1) lib/action_controller/metal.rb:262:in `dispatch'
 - actionpack (5.0.1) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
 - actionpack (5.0.1) lib/action_dispatch/routing/route_set.rb:32:in `serve'
 - actionpack (5.0.1) lib/action_dispatch/journey/router.rb:39:in `block in serve'
 - actionpack (5.0.1) lib/action_dispatch/journey/router.rb:26:in `serve'
 - actionpack (5.0.1) lib/action_dispatch/routing/route_set.rb:725:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - omniauth (1.3.1) lib/omniauth/strategy.rb:186:in `call!'
 - omniauth (1.3.1) lib/omniauth/strategy.rb:164:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - omniauth (1.3.1) lib/omniauth/builder.rb:63:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/rack/agent_hooks.rb:30:in `traced_call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/rack/browser_monitoring.rb:32:in `traced_call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - route_downcaser (1.2.0) lib/route_downcaser/downcase_route_middleware.rb:46:in `_call'
 - route_downcaser (1.2.0) lib/route_downcaser/downcase_route_middleware.rb:9:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - mobvious (0.3.2) lib/mobvious/manager.rb:25:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
 - warden (1.2.6) lib/warden/manager.rb:34:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - rack (2.0.1) lib/rack/etag.rb:25:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - rack (2.0.1) lib/rack/conditional_get.rb:38:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - rack (2.0.1) lib/rack/head.rb:12:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - rack (2.0.1) lib/rack/session/abstract/id.rb:222:in `context'
 - rack (2.0.1) lib/rack/session/abstract/id.rb:216:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - actionpack (5.0.1) lib/action_dispatch/middleware/cookies.rb:613:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - activerecord (5.0.1) lib/active_record/migration.rb:553:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - actionpack (5.0.1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
 - activesupport (5.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
 - actionpack (5.0.1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - actionpack (5.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - actionpack (5.0.1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
 - better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
 - better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - actionpack (5.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - web-console (3.4.0) lib/web_console/middleware.rb:135:in `call_app'
 - web-console (3.4.0) lib/web_console/middleware.rb:28:in `block in call'
 - web-console (3.4.0) lib/web_console/middleware.rb:18:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - actionpack (5.0.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - chewy (0.8.4) lib/chewy/railtie.rb:17:in `block in call'
 - chewy (0.8.4) lib/chewy/strategy.rb:60:in `wrap'
 - chewy (0.8.4) lib/chewy.rb:179:in `strategy'
 - chewy (0.8.4) lib/chewy/railtie.rb:17:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - railties (5.0.1) lib/rails/rack/logger.rb:36:in `call_app'
 - railties (5.0.1) lib/rails/rack/logger.rb:24:in `block in call'
 - activesupport (5.0.1) lib/active_support/tagged_logging.rb:69:in `block in tagged'
 - activesupport (5.0.1) lib/active_support/tagged_logging.rb:26:in `tagged'
 - activesupport (5.0.1) lib/active_support/tagged_logging.rb:69:in `tagged'
 - railties (5.0.1) lib/rails/rack/logger.rb:24:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - request_store (1.3.2) lib/request_store/middleware.rb:9:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - actionpack (5.0.1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - rack (2.0.1) lib/rack/method_override.rb:22:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - rack (2.0.1) lib/rack/runtime.rb:22:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - activesupport (5.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - actionpack (5.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - actionpack (5.0.1) lib/action_dispatch/middleware/static.rb:136:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - railties (5.0.1) lib/rails/engine.rb:522:in `call'
 - newrelic_rpm (3.17.2.327) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
 - puma (3.6.2) lib/puma/configuration.rb:225:in `call'
 - puma (3.6.2) lib/puma/server.rb:578:in `handle_request'
 - puma (3.6.2) lib/puma/server.rb:415:in `process_client'
 - puma (3.6.2) lib/puma/server.rb:275:in `block in run'
 - puma (3.6.2) lib/puma/thread_pool.rb:116:in `block in spawn_thread'

1 个答案:

答案 0 :(得分:0)

从ajax中试试这个:

  1. 更改url: '/retrieve-location.json'
  2. 更改dataType: "json"