【问题标题】:HTTP Post with ionic 3带有离子 3 的 HTTP 发布
【发布时间】:2018-07-30 16:58:11
【问题描述】:

我正在尝试调用本地 rest 网络服务来测试一个简单的登录功能 我已经尝试过使用 Get 函数,现在它工作正常我正在尝试使用 Post 函数,但我不知道如何将参数传递给我的请求

import { HttpClient,HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {Storage} from "@ionic/storage";
// import { HttpParams } from '@angular/http';

/*
  Generated class for the RestProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
  */
  @Injectable()
  export class RestProvider {

    apiUrl = 'http://localhost:8080/CrunchifyTutorials/api/crunchifyService';
    user : any;

    constructor(public http: HttpClient, public storage: Storage) {
      console.log('Hello RestProvider Provider');
    //console.log(this.storage.get('user'));
  }

//   getUsers() {
//   return new Promise(resolve => {
//     this.http.get(this.apiUrl).subscribe(data => {
//       resolve(data);
//       this.storage.set('user', data);
//     }, err => {
//       console.log(err);
//     });
//   });
// }
  //--------------------------------------------
  getUsers() {
  //   let data = new URLSearchParams();
  // data.append('user', 'khaled');
  // data.append('pass', 'khaled');
  //data =Object.assign(urlSearchParams, {search: "person"});

  const data = new HttpParams()
  .set('user', 'khaled').set('pass', 'khaled');

  this.http
  .post(this.apiUrl, data, {headers:{'Content-Type': 'application/json'}})
  .subscribe(data => {
    alert('ok');
  }, error => {
    console.log(error.json());
  });
}
}

还有 Java 部分

@POST
	@Path("/crunchifyService")
	@Produces("application/json")
	@Consumes("application/json")
	public Modele add (String user, String pass) throws ClassNotFoundException, SQLException{
			
		Auth_Ctrl auth = new Auth_Ctrl();
		int result =auth.connect(user, pass);
		
		return auth.info(result);
	}

问题是当我调试服务器部分时,我的所有参数都是空的,并且客户端部分没有错误

【问题讨论】:

  • data 是 JSON 对象吗?
  • 不,它不是 JSON 对象,我如何将其转换为 JSON 对象
  • 我同意 Chris 提供的答案,但如果您确实使用查询参数,则需要在 Angular 代码中使用 HttpParamsURLSearchParams 属于已弃用的HttpModule

标签: java angular web-services typescript ionic3


【解决方案1】:

您正在使用@QueryParam,它使用获取参数。这些参数应该包含在 url 中。 http.post 方法将数据编码为请求正文中的application/json

您应该更改其中任何一个以匹配另一个。 我的建议是将这里的 java 更改为使用 applications/json,因为在 url 中输入密码的安全问题

【讨论】:

  • 感谢您的帮助,所以我已经用我现在使用的新代码HttpParams 编辑了我的帖子,并在我的方法之前添加了@Consumes("application/json"),但现在的问题是我得到了user=khaled&pass=khaled作为我的第一个参数,而不是我的第二个参数
【解决方案2】:

对于可能感兴趣的人,我这样修改了我的代码 我添加了一个包含两个字段用户的类“日志”,并在 thislink 之后传入我的网络服务 然后我就这样修改了我的方法

@POST
	@Path("/crunchifyService")
	@Produces("application/json")
	@Consumes("application/json")
	public Modele auth (Log user) throws ClassNotFoundException, SQLException{
			
		Auth_Ctrl auth = new Auth_Ctrl();
		int result =auth.connect(user.user, user.pass);
		
		return auth.info(result);
	}
我注意到我需要将jackson-mapper-asl 添加到我的类路径中 之后一切正常

【讨论】:

    猜你喜欢
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多