【问题标题】:Confusion: AngularJS $http POST response is returning HTML content of index file困惑:AngularJS $http POST 响应返回索引文件的 HTML 内容
【发布时间】:2016-08-17 21:11:37
【问题描述】:

我正在使用 AngularJS,它使用 Ruby on Rails API 进行身份验证。我在 AngularJS 中使用 $http 服务使用 POST 请求向后端注册用户,但是,我很困惑,因为我得到的响应总是以 HTML 形式返回,它是索引页面,而不是来自的实际数据导轨 API。我在本地环境中测试这个,AngularJS 应用程序在 localhost:9000 上运行,Rails API 在 localhost:3000 上。 AngularJS 使用代理将请求定向到端口 3000。

我已经使用 curl 命令测试了 Rails API 的响应:

$ curl -H "Content-Type: application/json" -d '{"user":{"email":"hi@hi.com","password":"12345678"}}' -X POST http://localhost:9000/api/users.json

从服务器返回响应为:

{"success":true,"info":"Registered","data":{"user":{"id":5,"created_at":"2016-04-24T14:13:24.289Z","updated_at":"2016-04-24T14:13:24.298Z","email":"hi@hi.com","authentication_token":"-49zyhbCiddYdjssQx6i"},"auth_token":"-49zyhbCiddYdjssQx6i"}}

这是前端的代码,位于 AngularJS 控制器中:

 $scope.register = function() {
            $http({
                url: '/api/users',
                method: 'POST',
                data: {
                    user: $scope.user
                }
            }).then(
                // success
                function(response) {
                    console.log(response.data);
                    console.log(response.auth_token);
                    tokenHandler.set( response.auth_token );
                    $location.path('/');
                },
                // error
                function(response) {
                    $scope.user.errors = response;
                });
        };

这是服务器响应代码:

class Users::RegistrationsController < Devise::RegistrationsController
respond_to :json

def create
build_resource(sign_up_params)

if resource.save
  sign_in resource
  render status: 200,
         json: {
             success: true,
             info: "Registered",
             data: {
                 user: resource,
                 auth_token: current_user.authentication_token
             }
         }
else
  # Otherwise fail
  render status: :unprocessable_entity,
         json: {
             success: false,
             info: resource.errors,
             data: {}
         }
end
end
end

但是,当我调用 register 方法时,响应总是在 HTML 和我前端的索引文件中。

我错过了什么吗?

【问题讨论】:

  • 在您的curl 调用中,您的URL 是http://localhost:9000/api/users.json,而在您的$http 调用中,URL 是/api/users
  • 我尝试在 $http 调用中使用localhost:9000/api/users.json,但它的响应相同,索引页面的 HTML。

标签: ruby-on-rails angularjs ruby rest restful-authentication


【解决方案1】:

代理似乎将 localhost:9000 调用定向到服务器,但响应始终是前端索引文件的 HTML 内容。我不确定实际问题是什么,也不确定这种类型的东西。我通过将 $http POST 请求发送到 localhost:3000 而不是 localhost:9000 来解决此问题。

【讨论】:

    【解决方案2】:

    似乎 Angular 没有发出期望 json 的请求。而且 rails 需要那个明确的(你的 curl 命令明确要求 json 格式)

    在您的角度代码中,您可以尝试将 url 从 url: '/api/users' 更改为 url: '/api/users.json'。您还可以检查如何从 Angular 发送预期的 application/json 内容类型。

    【讨论】:

      【解决方案3】:

      不确定我是否理解发生了什么。但是从您的帖子的外观来看,这是我所理解的(我的英语并非完美无缺):

      • 当外部调用时(Angular 应用程序收到请求),该请求被转发到端口 3000(代理)。因此,为什么您会从“curl”调用中得到适当的响应。
      • 当 Angular 应用发出请求时,它不会被转发。因此,为什么要求 /api/users 会为您提供 Angular 应用程序索引(我想有一个故障安全路由来索引或解释该行为的某种原因)。

      如果有任何意义,你能测试一下吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-17
        • 2016-10-03
        • 1970-01-01
        • 1970-01-01
        • 2018-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多