scala返回创建对象的http请求响应

时间:2017-08-01 06:46:55

标签: scala rest playframework slick

我有一个具有以下后端技术的应用程序:在playframework中使用scala / slick。前端和后端通过REST进行通信。

现在我想要做的只是将创建/插入(更新)的行返回到我的应用程序的前端。我想做这样的事情:

def createClient = Action.async { implicit request =>
    request.body.asJson.map(_.validate[ClientModel]) match {
      case c: JsSuccess[ClientModel] =>
        clientDTO.createClient(c.get).map{
          cnt => Ok(Json.obj("id" -> cnt.id))
        }.recover {
          case e: Exception => BadRequest("Could ssnotsdreate client")
        }
    }
  }

我的代码编译但是在运行时它给了我这个错误消息:

XMLHttpRequest cannot load http://localhost:9002/clients. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 500.

我读过将CORS添加到我的应用程序中,但更愿意解决它。我认为必须有一种正确,优雅的方式将刚刚创建/插入的对象返回到前端,因为它应该是任何客户端 - 服务器通信的核心功能。

我对scala比较陌生,所以请不要挂断我的代码,而是将其视为伪代码。这是一个相当普遍的问题。谢谢!

0 个答案:

没有答案