我正在尝试使用 HTTP cordova 插件发出发布请求。但是,由于某种原因,服务器端使用的 JSON 数据的格式不正确(json 括号)。有人可以帮我吗?
进口:
import { HTTP } from '@ionic-native/http';
请求实现:
public sendData(sufix, json) {
return new Promise((resolve, reject) => {
this.http.post(URL+sufix, JSON.stringify(json), {'Content-Type': 'application/json'}).then(result => {
resolve(result.data);
}).catch(error => {
reject(error);
});
});
}
发送的json:
{名称:'测试'}
服务器收到的内容:
=%7B%22名称%22%3A%22Test%22%7D
服务器实现:
@Path("/register")
public class RegisterEndPoint {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response registerUser(UserDTO userDTO) {
// Create DAO for persistence
FactoryDAO factory = new FactoryDAO();
UserDAO userDAO = factory.getUserDAO();
// Create user to be persisted
if (!userDAO.userExist(userDTO.getEmail())) {
User user = new User();
user.setPassword(userDTO.getPassword());
user.setEmail(userDTO.getEmail());
user.setName(userDTO.getName());
userDAO.persist(user);
userDAO.commit();
return Response.status(200).build();
}
return Response.status(405).entity(new ErrorDTO("User already registered!")).build();
}
}