<bdo id='PoddW'></bdo><ul id='PoddW'></ul>
      <legend id='PoddW'><style id='PoddW'><dir id='PoddW'><q id='PoddW'></q></dir></style></legend>

      <small id='PoddW'></small><noframes id='PoddW'>

    1. <tfoot id='PoddW'></tfoot>

    2. <i id='PoddW'><tr id='PoddW'><dt id='PoddW'><q id='PoddW'><span id='PoddW'><b id='PoddW'><form id='PoddW'><ins id='PoddW'></ins><ul id='PoddW'></ul><sub id='PoddW'></sub></form><legend id='PoddW'></legend><bdo id='PoddW'><pre id='PoddW'><center id='PoddW'></center></pre></bdo></b><th id='PoddW'></th></span></q></dt></tr></i><div id='PoddW'><tfoot id='PoddW'></tfoot><dl id='PoddW'><fieldset id='PoddW'></fieldset></dl></div>

        播放 2.0.1 并设置 Access-Control-Allow-Origin

        时间:2023-10-13
            <bdo id='NsF7N'></bdo><ul id='NsF7N'></ul>

            <small id='NsF7N'></small><noframes id='NsF7N'>

            <legend id='NsF7N'><style id='NsF7N'><dir id='NsF7N'><q id='NsF7N'></q></dir></style></legend>

            1. <i id='NsF7N'><tr id='NsF7N'><dt id='NsF7N'><q id='NsF7N'><span id='NsF7N'><b id='NsF7N'><form id='NsF7N'><ins id='NsF7N'></ins><ul id='NsF7N'></ul><sub id='NsF7N'></sub></form><legend id='NsF7N'></legend><bdo id='NsF7N'><pre id='NsF7N'><center id='NsF7N'></center></pre></bdo></b><th id='NsF7N'></th></span></q></dt></tr></i><div id='NsF7N'><tfoot id='NsF7N'></tfoot><dl id='NsF7N'><fieldset id='NsF7N'></fieldset></dl></div>
                <tbody id='NsF7N'></tbody>

                1. <tfoot id='NsF7N'></tfoot>
                2. 本文介绍了播放 2.0.1 并设置 Access-Control-Allow-Origin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个 Play 2.0.1 应用程序,我想使用托管在其他域上的 Javascript 来调用它.我的 Javascript 调用失败:

                  I have a Play 2.0.1 application that I want to call using Javascript hosted on other domains. My Javascript call is failing with:

                  Origin http://mydomain.com is not allowed by Access-Control-Allow-Origin.
                  

                  我找到了一些关于如何在 Play 1 中设置正确 HTTP 标头的示例,但没有找到适用于 Play 2.0.1 的任何内容.阅读文档 (http://www.playframework.org/documentation/2.0.2/JavaResponse) 后,我尝试了以下操作以使事情正常进行:

                  I have found a number of examples of how to set the correct HTTP header in Play 1, but have not found anything for Play 2.0.1. After reading the documentation (http://www.playframework.org/documentation/2.0.2/JavaResponse) I've tried the following just to get things working:

                  public static Result myJsonWebService() {
                    ...
                    response().setHeader("Access-Control-Allow-Origin", "*");
                    return ok(toJson(jsonObject));
                  }
                  

                  但我的 JS Web 服务调用仍然失败.

                  but my JS web service call is still failing.

                  我需要做什么才能让它工作?

                  What do I need to do to get this working?

                  推荐答案

                  对于 Scala 的人来说,这是我目前正在使用的实现:

                  Just for Scala guys, this is the implementation I'm currently using:

                  import play.api.mvc._
                  import scala.concurrent._
                  import play.api.http.HeaderNames._
                  
                  /**
                   * Action decorator that provide CORS support
                   *
                   * @author Giovanni Costagliola, Nick McCready
                   */
                  case class WithCors(httpVerbs: String*)(action: EssentialAction) extends EssentialAction with Results {
                      def apply(request: RequestHeader) = {
                          implicit val executionContext: ExecutionContext = play.api.libs.concurrent.Execution.defaultContext
                          val origin = request.headers.get(ORIGIN).getOrElse("*")
                          if (request.method == "OPTIONS") { // preflight
                              val corsAction = Action {
                                  request =>
                                      Ok("").withHeaders(
                                          ACCESS_CONTROL_ALLOW_ORIGIN -> origin,
                                          ACCESS_CONTROL_ALLOW_METHODS -> (httpVerbs.toSet + "OPTIONS").mkString(", "),
                                          ACCESS_CONTROL_MAX_AGE -> "3600",
                                          ACCESS_CONTROL_ALLOW_HEADERS ->  s"$ORIGIN, X-Requested-With, $CONTENT_TYPE, $ACCEPT, $AUTHORIZATION, X-Auth-Token",
                                          ACCESS_CONTROL_ALLOW_CREDENTIALS -> "true")
                              }
                              corsAction(request)
                          } else { // actual request
                              action(request).map(res => res.withHeaders(
                                  ACCESS_CONTROL_ALLOW_ORIGIN -> origin,
                                  ACCESS_CONTROL_ALLOW_CREDENTIALS -> "true"
                              ))
                          }
                      }
                  }
                  

                  要使用它,只需按以下方式装饰您的操作:

                  To use it just decorate your action in the following way:

                  def myAction = WithCors("GET", "POST") {
                    Action { request =>
                      ???
                    }
                  }
                  

                  这篇关于播放 2.0.1 并设置 Access-Control-Allow-Origin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 JavaScript 的基本身份验证 下一篇:捕获 XMLHttpRequest 跨域错误

                  相关文章

                  最新文章

                3. <legend id='QzsPi'><style id='QzsPi'><dir id='QzsPi'><q id='QzsPi'></q></dir></style></legend>
                  <tfoot id='QzsPi'></tfoot>

                    • <bdo id='QzsPi'></bdo><ul id='QzsPi'></ul>
                    <i id='QzsPi'><tr id='QzsPi'><dt id='QzsPi'><q id='QzsPi'><span id='QzsPi'><b id='QzsPi'><form id='QzsPi'><ins id='QzsPi'></ins><ul id='QzsPi'></ul><sub id='QzsPi'></sub></form><legend id='QzsPi'></legend><bdo id='QzsPi'><pre id='QzsPi'><center id='QzsPi'></center></pre></bdo></b><th id='QzsPi'></th></span></q></dt></tr></i><div id='QzsPi'><tfoot id='QzsPi'></tfoot><dl id='QzsPi'><fieldset id='QzsPi'></fieldset></dl></div>
                  1. <small id='QzsPi'></small><noframes id='QzsPi'>