scala - 播放 2.2.1 SimpleResult 4xx 响应主体是否可以通过 CORS XHR?

标签 scala https xmlhttprequest playframework-2.2

当我的 API 提供 4xx http 响应代码时,我正在尝试发送 JSON 响应正文。当代码为 200 时一切正常,但更改为 4xx 时,没有收到正文。 Chrome 的 Postman 显示 body 响应,但 XHR(Chrome,FF)没有。

有可能nginx CORS 在 XHR 中获取响应主体吗?正确设置(我将其设置为简单地 echo ...Allow-Origin: $http_origin)?我将 nginx 作为 proxy_pass 到 127.0.0.1:9001 上的实际 API 服务器。当我发送已知凭据时,CORS 和 Allow-Origin 都可以正常工作并显示 200 OK。如果我只是使用错误的密码,我会在 Firebug 中看到错误状态代码,但 XHR 对象(也是 jQuery 的 $.ajax)的状态 = 0,即使请求到达 API 服务器也是如此。

至少我希望能够在 XHR 对象中获取 4xx 状态代码,这样我至少可以显示某些 4xx 代码的一般信息,而不是一般的“它不起作用”。这也不可用,我总是得到 status=0,即使 API 被命中并且正确的响应代码被记录在 nginx 中。

更新 3/23 Javascript XHR 代码:

var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://arinna-api/v1/401', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onload = function () {
    // do something to response
    console.log(this);
};

播放框架代码返回状态 200 OK JSON 内容类型响应体:

SimpleResult(
  header = ResponseHeader(200, Map(CONTENT_TYPE -> "application/json")),
  body = Enumerator("{\"error\":\"invalid grant\"}".getBytes())
)

200 OK 按预期工作:

HTTP 200 OK Works but is not REST

将 HTTP 代码更改为 401 会导致 XMLHttpRequest 无法接收到响应正文(xhr.status 也为 0 而不是 401):

SimpleResult(
  header = ResponseHeader(401, Map(CONTENT_TYPE -> "application/json")),
  body = Enumerator("{\"error\":\"invalid grant\"}".getBytes())
)

xhr.send('username=username&password=badpassword&grant_type=password&client_id=mobile_platform');

HTTP 401 no body parsed from response

Postman按预期工作:

enter image description here

是否可以通过 XHR 获取 4xx 响应主体和状态代码?根据W3C Status Code Definitions在 HTTP 4xx 上:

Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition. These status codes are applicable to any request method. User agents SHOULD display any included entity to the user.

然而,根据this discussion :

2) Browsers do not display the text/html response body of a HTTP 401 response, instead, they just pop up a modal authentication dialog (until "cancel" is pressed).

3) Servers do not send a meaningful response body with the 401 status as browsers do not display it anyway.

最佳答案

下面的代码按预期运行:

Application.scala

object Application extends Controller {

  def index = Action { request =>
    Ok(views.html.index())
  }

  def indexPost = Action { request =>
    Unauthorized("test").withHeaders(ACCESS_CONTROL_ALLOW_ORIGIN -> "*")
  }
}

路线

GET    /   controllers.Application.index
POST   /   controllers.Application.indexPost

index.html.scala

@()

@main("Welcome to Play 2.1") {
  <script type="text/javascript">
      var xhr = new XMLHttpRequest();
      xhr.open('POST', 'http://localhost:9000/');
      xhr.onload = function () {
      console.log(this);
    };
    xhr.send();
  </script>
}

文本 "test" 显示在响应中(Opera、Chrome 和 Firefox)。请注意,我在 90009001 端口运行了两次 Play,并使用了 Play 2.2.1。然后从 Play at 9001 我向 9000 发出请求。

关于scala - 播放 2.2.1 SimpleResult 4xx 响应主体是否可以通过 CORS XHR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22570550/

相关文章:

javascript - 如何知道请求何时开始?

javascript - DOM字符串解析器

ssl - Fiddler 如何删除 HTTPS 扩展?

java - Gradle:编译spark-scala程序时出现java.lang.StackOverflowError

scala - 当我尝试在 Spark-Scala 中编译 SBT 时,错误 package org.apache.spark.sql is not a value

json - Play2 向 JsObject 添加新字段

java - Apache Http 客户端 SSL 证书错误

ssl - 我想为我的测试环境创建 SSL 证书

ajax - 我们如何将响应 header 设置为 Access-Control-Allow-Origin : * in jQuery Ajax

scala - 为值和该值的仿函数编写单个隐式类