【问题标题】:Angularjs - Play Framework. Unable to receive response in Http postAngularjs - 播放框架。无法在 Http 帖子中接收响应
【发布时间】:2015-08-10 18:35:43
【问题描述】:

在 angularjs 中没有收到简单的 http 响应,但在 curl 请求中成功返回。我启用了 CORS,并且在服务器中收到了 post 参数,但返回的响应始终为空白。

这是我的请求部分:

$http({
            method: 'POST',
            url: 'http://localhost:9000/adminAuth',
            data: $.param({email:username,password:password})
            }).then(function(response,status){
                auth.user=response.data;
                $cookieStore.put('user',auth.user);
                return auth.user;
            }, function(error) {
                console.log(error);
            });  

控制台中打印的错误为:

【问题讨论】:

  • 你会尝试添加这行数据吗:{ contentType: "application/json;charset=utf-8", email:username,password:password },
  • @Sunny_Sid 我得到了同样的空白回复。
  • 请看我下面的帖子。可能对你有帮助。

标签: angularjs playframework playframework-2.0 angularjs-service angularjs-routing


【解决方案1】:

我使用以下代码实现了这一点:-

$http({
                method: 'POST',
                data: {
                    contentType: "application/json;charset=utf-8",
                    ids: Id,
                    Action: actionid,
                    isSelectedAll: true
                },
                url: 'Doctor/Doctor/AppointmentStatusUpdate',
                responseType: "application/json;charset=utf-8"
            })
                .success(function (data, status, headers, config) {
  // do anything
  })

【讨论】:

  • 我已将内容类型设置为 'application/x-www-form-urlencoded'。如果设置为 'application/json' 并以 404 结尾,它将作为 OPTIONS 发送请求。
  • 确保你的控制器应该有匿名属性并且方法名应该由 [httpPost] 定义
【解决方案2】:

根据调试消息,您似乎正在尝试向其他来源发出请求。您需要将Access-Control-Allow-Origin 添加为*,或类似的内容。问题是您的浏览器阻止了响应(对于security reasons)。我不知道您使用的是哪个 API,但在 Scala 中,您需要在响应中添加以下内容:

Ok("This is my response")
  .withHeaders("Access-Control-Allow-Origin" -> "*")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多