【问题标题】:AngularJS $http.post method passing null arguments to JAX-RS APIAngularJS $http.post 方法将空参数传递给 JAX-RS API
【发布时间】:2016-05-05 14:58:09
【问题描述】:

我正在尝试使用 AngularJS 的 post 方法传递一些参数:

function Create(email, password, phoneNumber) {
        return $http.post('resources/users/register', {email: email, password: password, phoneNumber: phoneNumber}).then(handleSuccess, handleError('Error creating user'));
    }

到我的 JAX-RS 方法:

@POST
@Produces(MediaType.APPLICATION_JSON )
@Path("/register")
public Response register(@FormParam("email") String email, @FormParam("password") String password,
                         @FormParam("phoneNumber") String phoneNumber) {

我在 $http.post 之前和 register 方法的开头有记录器,当 web 端的参数包含信息时,服务器上的每个参数都是 null -> 我试图使用 curl 方法 - 一切都很好!

有人有想法吗?

【问题讨论】:

  • cURL 默认发送 application/x-www-form-urlecoded 数据。这是您在使用@FormParam 的资源方法中所期望的那种数据。 Angular 默认发送 JSON 数据。你需要让它们匹配。如果要使用表单数据,请设置 Angular 以使用表单数据。否则设置你的资源方法接受 JSON

标签: java angularjs jersey jax-rs


【解决方案1】:

尝试像这样更改 json 对象:

{'email': email, 'password': password, 'phoneNumber': phoneNumber}

Javascript var ---> 字符串名称值

JSON 数据以名称/值对的形式写入。名称/值对包括 字段名(双引号),后跟冒号,后跟 价值:

【讨论】:

  • 好吧,在萤火虫中,我试图查看构造的 JSON,它看起来像 {"email": "email", "password": "password", "phoneNumber": "phoneNumber" },但我尝试使用您的建议构建 JSON - 它没有帮助 - 我得到完全相同的响应。
【解决方案2】:

我敢打赌这是你的情况:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/register")

你错过了@Consumes(MediaType.APPLICATION_JSON)

【讨论】:

  • 不幸的是,这对我没有帮助
猜你喜欢
  • 2011-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-18
  • 2014-03-21
相关资源
最近更新 更多