Evan Hu
Evan Hu

Reputation: 60

spring and angular2 how to request the post data with params?

angular2 how request the data with the params,and spring @requestParam or @requestBody like this..

/**
 * 后台账户登录
 *
 * @param userName userName
 * @param password password
 * @return result
 */
@RequestMapping(value = "login", method = RequestMethod.POST)
public AdminModel login(@RequestParam String userName, @RequestParam String password) {
    AdminModel adminModel = service.findAdminUserByUserName(userName);
    if (adminModel == null) {
        return null;
    }
    if (MD5Util.encode(password, adminModel.getSalt()).equals(adminModel.getPassword())) {
        return null;
    }
    return adminModel;
}

I have tried many times,but I can't get the correct method.

Upvotes: 2

Views: 1849

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209004

Use URLSearchParams. This will automatically set the Content-Type to application/x-www-form-urlencoded and encode the params into the correct format.

import { URLSearchParams } from '@angular/http';

const params = new URLSearchParams();
params.set('userName', userName);
params.set('password', password);

this.http.post(url, params)

Upvotes: 2

Related Questions