离子应用程序适用于服务器但不适用于ios

时间:2016-04-18 14:50:30

标签: ruby-on-rails ionic-framework cors ios-simulator rails-api

我有一个完全与ionic serve一起使用的离子应用,但不会始终使用ionic emulateionic emulate -lc。该应用程序将加载两者。第一页是登录屏幕的链接。链接在两者中都有效,并进入登录屏幕。使用ionic serve,用户可以成功登录。使用ionic emulate,按登录会生成以下服务器日志(Rails,非离子版):

Started POST "/api/auth/sign_in" for ::1 at 2016-04-18 10:37:38 -0400
Processing by Api::SessionsController#create as HTML
  Parameters: {"email"=>"facility@example.com", "password"=>"[FILTERED]", "session"=>{"email"=>"facility@example.com", "password"=>"[FILTERED]"}}
  FacilityUser Load (0.1ms)  SELECT  "facility_users".* FROM "facility_users" WHERE (email = 'facility@example.com' AND provider='email')  ORDER BY "facility_users"."id" ASC LIMIT 1
   (0.1ms)  begin transaction
  SQL (0.2ms)  UPDATE "facility_users" SET "tokens" = ?, "updated_at" = ? WHERE "facility_users"."id" = ?  [["tokens", "{\"vyBzhBqltxsx5KzZIvpnlA\":{\"token\":\"$2a$10$aWmHJTG8QcfUQJvB5wG9bOSgCLDi7kCJQlknOyZk0dEG1SwrYVpTu\",\"expiry\":1462199088},\"MaSuqSB_IYB3030XutiEEQ\":{\"token\":\"$2a$10$HSWVZ5821E/5pPpX2yRMAOa0LZ6w36AQH3tPwA/zBF9jjnrUdgztS\",\"expiry\":1462199119},\"Wx0wza7cApR_6lT0FYdbTA\":{\"token\":\"$2a$10$N/PE3TXrIGLi6yk2EH1M7e9BFPFo.DRkwSSlDLia8z0m/CeBGtHRy\",\"expiry\":1462199144},\"ur2tP2OtJOEMU5LoghEW0g\":{\"token\":\"$2a$10$ZE9Y3Ngd.VeR6Iug9iqQM.VNp9WKWrhxmKIiouIyo53ffhJGEIMzW\",\"expiry\":1462199180},\"qTPfmajhEnkYJD9YtoKnJw\":{\"token\":\"$2a$10$5woDc19MlfHwVztm5Gt1oeb0mvTJLnOXmqSZ0vtEYJYBiiZBhBCm.\",\"expiry\":1462199219},\"j3EIZGfC1EPKgsqIyA4ZAA\":{\"token\":\"$2a$10$j9lk7SdNi9VzCQvfq.zui.tQbE8i8FpRcu7nd6oOakqoFOu4DcsGK\",\"expiry\":1462199227},\"Re5xw7BGJDjF_VfkPvk1ig\":{\"token\":\"$2a$10$DltH/JoBIYmWF/ikwZVUpevJFIF0h7rjmPhwTAPa2pL07GBQGx0XK\",\"expiry\":1462199858}}"], ["updated_at", "2016-04-18 14:37:38.767170"], ["id", 1]]
   (1.3ms)  commit transaction
   (0.1ms)  begin transaction
  SQL (0.1ms)  UPDATE "facility_users" SET "last_sign_in_at" = ?, "current_sign_in_at" = ?, "sign_in_count" = ?, "updated_at" = ? WHERE "facility_users"."id" = ?  [["last_sign_in_at", "2016-04-18 14:27:07.180638"], ["current_sign_in_at", "2016-04-18 14:37:38.770547"], ["sign_in_count", 7], ["updated_at", "2016-04-18 14:37:38.771425"], ["id", 1]]
   (0.5ms)  commit transaction
[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (0.07ms)
Completed 200 OK in 170ms (Views: 0.4ms | ActiveRecord: 2.4ms)

正如您在上面的代码中看到的,服务器正在发回200响应,因此这必须是客户端/离子/模拟器问题。事实上,它也适用于ionic serve,这也让我相信它的客户端而不是服务器。如果我运行ionic emulate -lc,我会在服务器日志中获得以下内容:

Started OPTIONS "/api/auth/sign_in" for ::1 at 2016-04-18 10:42:14 -0400

ActionController::RoutingError (No route matches [OPTIONS] "/api/auth/sign_in"):
  actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.6) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  rack-cors (0.4.0) lib/rack/cors.rb:80:in `call'
  railties (4.2.6) lib/rails/engine.rb:518:in `call'
  railties (4.2.6) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /Users/philipgreenwald/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
  /Users/philipgreenwald/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
  /Users/philipgreenwald/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'


  Rendered /Users/philipgreenwald/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.9ms)
  Rendered /Users/philipgreenwald/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (4.3ms)
  Rendered /Users/philipgreenwald/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.0ms)
  Rendered /Users/philipgreenwald/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.8ms)
  Rendered /Users/philipgreenwald/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (89.6ms)

所以这就是有趣的:使用Ionic emulate它会发送一个帖子请求,但是ionic emulate -lc会发送一个OPTIONS请求。无论哪种方式,它永远不会在模拟器内部工作。它仅适用于ionic serve。这是另一个有趣的事情,登录前的离子日志显示了这一点:

0     515446   log      deviceready has not fired after 5 seconds.
1     515447   log      Channel not fired: onCordovaInfoReady

最后,这是从ionic serve运行的服务器日志:

Started POST "/api/auth/sign_in" for ::1 at 2016-04-18 10:47:28 -0400
Processing by Api::SessionsController#create as HTML
  Parameters: {"email"=>"facility@example.com", "password"=>"[FILTERED]", "session"=>{"email"=>"facility@example.com", "password"=>"[FILTERED]"}}      FacilityUser Load (0.3ms)  SELECT  "facility_users".* FROM "facility_users" WHERE (email = 'facility@example.com' AND provider='email')  ORDER BY "facility_users"."id" ASC LIMIT 1
   (0.1ms)  begin transaction
  SQL (0.2ms)  UPDATE "facility_users" SET "tokens" = ?, "updated_at" = ? WHERE "facility_users"."id" = ?  [["tokens", "{\"vyBzhBqltxsx5KzZIvpnlA\":{\"token\":\"$2a$10$aWmHJTG8QcfUQJvB5wG9bOSgCLDi7kCJQlknOyZk0dEG1SwrYVpTu\",\"expiry\":1462199088},\"MaSuqSB_IYB3030XutiEEQ\":{\"token\":\"$2a$10$HSWVZ5821E/5pPpX2yRMAOa0LZ6w36AQH3tPwA/zBF9jjnrUdgztS\",\"expiry\":1462199119},\"Wx0wza7cApR_6lT0FYdbTA\":{\"token\":\"$2a$10$N/PE3TXrIGLi6yk2EH1M7e9BFPFo.DRkwSSlDLia8z0m/CeBGtHRy\",\"expiry\":1462199144},\"ur2tP2OtJOEMU5LoghEW0g\":{\"token\":\"$2a$10$ZE9Y3Ngd.VeR6Iug9iqQM.VNp9WKWrhxmKIiouIyo53ffhJGEIMzW\",\"expiry\":1462199180},\"qTPfmajhEnkYJD9YtoKnJw\":{\"token\":\"$2a$10$5woDc19MlfHwVztm5Gt1oeb0mvTJLnOXmqSZ0vtEYJYBiiZBhBCm.\",\"expiry\":1462199219},\"j3EIZGfC1EPKgsqIyA4ZAA\":{\"token\":\"$2a$10$j9lk7SdNi9VzCQvfq.zui.tQbE8i8FpRcu7nd6oOakqoFOu4DcsGK\",\"expiry\":1462199227},\"Re5xw7BGJDjF_VfkPvk1ig\":{\"token\":\"$2a$10$DltH/JoBIYmWF/ikwZVUpevJFIF0h7rjmPhwTAPa2pL07GBQGx0XK\",\"expiry\":1462199858},\"X2nb-s3j4BG9z8AFOplHcw\":{\"token\":\"$2a$10$mfeJvEtSlfFRgRuja5LVQ.VVn9I8JvudgEh7/02lI/kQ2E1csDe2S\",\"expiry\":1462200448}}"], ["updated_at", "2016-04-18 14:47:28.872136"], ["id", 1]]
   (1.3ms)  commit transaction
   (0.1ms)  begin transaction
  SQL (0.2ms)  UPDATE "facility_users" SET "last_sign_in_at" = ?, "current_sign_in_at" = ?, "sign_in_count" = ?, "updated_at" = ? WHERE "facility_users"."id" = ?  [["last_sign_in_at", "2016-04-18 14:37:38.770547"], ["current_sign_in_at", "2016-04-18 14:47:28.875744"], ["sign_in_count", 8], ["updated_at", "2016-04-18 14:47:28.876805"], ["id", 1]]
   (0.5ms)  commit transaction
[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (0.07ms)
Completed 200 OK in 182ms (Views: 0.5ms | ActiveRecord: 2.6ms)

似乎ionic serveionic emulate从服务器获得相同的响应,而ionic emulate -lc获得了不同的响应。无论哪种方式,即使ionic emulateionic serve的服务器响应相同,它也仅适用于ionic serve。如何让应用与ionic emulateionic emulate -lc一起使用?

1 个答案:

答案 0 :(得分:0)

经过多次挖掘,我终于来到这篇文章中,完美地回答了这个问题:

http://blog.ionic.io/handling-cors-issues-in-ionic/

基本上,将api列入白名单可以解决所有问题。另外,在Rails中,我不得不添加

UPDATE t1 
SET threshold = 
    (select value * 100 / t2.sumValue
    from (select sum(value) sumValue from t1) t2
    )

到api控制器。事实证明,这个问题毕竟是后端。