离子应用程序适用于服务器,但不适用于 ios 的模拟器

ionic app works with server but not in emulator for ios

我有一个 ionic 应用程序可以完全与 ionic serve 一起工作,但不能一直与 ionic emulateionic emulate -lc 一起工作。该应用程序将同时加载。第一页是登录屏幕的 link。 link 在两者中都有效并转到登录屏幕。使用 ionic serve,用户可以成功登录。使用 ionic emulate,按登录会导致以下服务器日志(Rails,而不是 Ionic):

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\":\"a$aWmHJTG8QcfUQJvB5wG9bOSgCLDi7kCJQlknOyZk0dEG1SwrYVpTu\",\"expiry\":1462199088},\"MaSuqSB_IYB3030XutiEEQ\":{\"token\":\"a$HSWVZ5821E/5pPpX2yRMAOa0LZ6w36AQH3tPwA/zBF9jjnrUdgztS\",\"expiry\":1462199119},\"Wx0wza7cApR_6lT0FYdbTA\":{\"token\":\"a$N/PE3TXrIGLi6yk2EH1M7e9BFPFo.DRkwSSlDLia8z0m/CeBGtHRy\",\"expiry\":1462199144},\"ur2tP2OtJOEMU5LoghEW0g\":{\"token\":\"a$ZE9Y3Ngd.VeR6Iug9iqQM.VNp9WKWrhxmKIiouIyo53ffhJGEIMzW\",\"expiry\":1462199180},\"qTPfmajhEnkYJD9YtoKnJw\":{\"token\":\"awoDc19MlfHwVztm5Gt1oeb0mvTJLnOXmqSZ0vtEYJYBiiZBhBCm.\",\"expiry\":1462199219},\"j3EIZGfC1EPKgsqIyA4ZAA\":{\"token\":\"a$j9lk7SdNi9VzCQvfq.zui.tQbE8i8FpRcu7nd6oOakqoFOu4DcsGK\",\"expiry\":1462199227},\"Re5xw7BGJDjF_VfkPvk1ig\":{\"token\":\"a$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 响应,因此这一定是 client-side/ionic/simulator 问题。它也适用于 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 发送 post 请求,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\":\"a$aWmHJTG8QcfUQJvB5wG9bOSgCLDi7kCJQlknOyZk0dEG1SwrYVpTu\",\"expiry\":1462199088},\"MaSuqSB_IYB3030XutiEEQ\":{\"token\":\"a$HSWVZ5821E/5pPpX2yRMAOa0LZ6w36AQH3tPwA/zBF9jjnrUdgztS\",\"expiry\":1462199119},\"Wx0wza7cApR_6lT0FYdbTA\":{\"token\":\"a$N/PE3TXrIGLi6yk2EH1M7e9BFPFo.DRkwSSlDLia8z0m/CeBGtHRy\",\"expiry\":1462199144},\"ur2tP2OtJOEMU5LoghEW0g\":{\"token\":\"a$ZE9Y3Ngd.VeR6Iug9iqQM.VNp9WKWrhxmKIiouIyo53ffhJGEIMzW\",\"expiry\":1462199180},\"qTPfmajhEnkYJD9YtoKnJw\":{\"token\":\"awoDc19MlfHwVztm5Gt1oeb0mvTJLnOXmqSZ0vtEYJYBiiZBhBCm.\",\"expiry\":1462199219},\"j3EIZGfC1EPKgsqIyA4ZAA\":{\"token\":\"a$j9lk7SdNi9VzCQvfq.zui.tQbE8i8FpRcu7nd6oOakqoFOu4DcsGK\",\"expiry\":1462199227},\"Re5xw7BGJDjF_VfkPvk1ig\":{\"token\":\"a$DltH/JoBIYmWF/ikwZVUpevJFIF0h7rjmPhwTAPa2pL07GBQGx0XK\",\"expiry\":1462199858},\"X2nb-s3j4BG9z8AFOplHcw\":{\"token\":\"a$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 一起使用?

经过大量挖掘,我终于找到了这篇完美回答问题的文章:

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

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

skip_before_filter :verify_authenticity_token

到 api 控制器。原来是后端的问题。