我正在尝试开发一个登录系统,并且我已经遵循了Ticketmonster的结构。 main.html中的菜单容器仅在登录后填写,内容填充登录模板,路由器立即呈现登录视图,并应用模板。单击登录按钮后,将创建一个Ajax请求并将其发送到其服务。
`
login:function(){
var username = $("#username");
var password = $("#password");
var userLoginRequest = {username:username.val(), password:password.val()};
$.ajax({url: (config.baseUrl + "rest/users"),
data:JSON.stringify(userLoginRequest),
type:"GET",
dataType:"json",
contentType:"application/json",
success:function (userId) {
alert(userId);
}}).error(function (error) {
if (error.status == 400 || error.status == 409) {
var errors = $.parseJSON(error.responseText).errors;
_.each(errors, function (errorMessage) {
$("#error").empty().append(errorMessage);
});
} else {
$("#error").empty().append("An error has occurred!");
}
});
}
`
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response authenticateUser(UserLoginRequest userLoginRequest){
User user = serviceUtility.getUserFromUsername(userLoginRequest.getUsername());
if (user == null){
return serviceUtility.badRequestResponse("No users with this username exist!");
} else if (!user.getPassword().equals(userLoginRequest.getPassword())) {
return serviceUtility.badRequestResponse("Incorrect Password!");
} else {
return Response.ok().entity(user.getId()).type(MediaType.APPLICATION_JSON_TYPE).build();
}
}
WARN [org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher](http-localhost / 127.0.0.1:8180-6)无法解析请求:java.lang.IllegalArgumentException:索引中查询中的非法字符38:localhost:8180 / MCC2 / rest / users?{%22username%22:%22wakas%22,%22password%22:%22Password%22}& _ = 1426754780833 在java.net.URI.create(URI.java:859)[rt.jar:1.7.0_03] ...
GET localhost:8180 / MCC2 / rest / users?{%22usernam ... 2,%22password%22:%22Password%22}& = 1426757495550
400错误请求
19ms
jquery-2.0.3.js(第7845行)
" NetworkError:400错误请求 - localhost:8180 / MCC2 / rest / users?{%22username%22:%22User%20Name%22,%22password%22:%22Password%22}& = 1426757495550"
用户?{... 7495550
SyntaxError:JSON.parse:JSON数据login.js第1行第1列的意外字符(第49行,第41行)
var errors = $ .parseJSON(error.responseText).errors;
POST localhost:8180 / MCC2 / rest / users 405方法不允许 8ms的 jquery-2.0.3.js(第7845行) " NetworkError:405方法不允许 - localhost:8180 / MCC2 / rest / users"
更新 虽然我用GET请求写了这篇文章,但我觉得它应该是一个POST请求,在这种情况下问题就是Firebug错误的原因。
此外,我在部署应用程序时遇到以下错误: 引起:org.hibernate.MappingException:无法确定类型:java.util.Set,在表:User,对于列:[org.hibernate.mapping.Column(userListIds)]
这是User(排除setter和getters):
`
@SuppressWarnings("serial")
@Entity
public class User implements Serializable {
@Id
@GeneratedValue
private Long id;
@NotNull
@Size(min = 5, max = 16)
@Pattern(regexp = "[a-zA-Z0-9_]*", message = "Must only contain small and capital letters, numbers, and underscore!")
@Column(unique=true)
private String username;
@NotNull
@Size(min = 5, max = 16)
private String password;
@NotNull
@Size(min = 1, max = 25)
@Pattern(regexp = "[^0-9]*", message = "Must not contain numbers")
private String firstName;
@NotNull
@Size(min = 1, max = 25)
@Pattern(regexp = "[^0-9]*", message = "Must not contain numbers")
private String lastName;
@NotNull
@Column(unique = true)
@NotEmpty
@Email(message = "Not a valid email format")
private String email;
@Size(min = 10, max = 12)
@Digits(fraction = 0, integer = 12)
private String phoneNumber;
@Size(min = 1, max = 300)
private String description;
// @NotNull <--commented since we don't have a setter
private Date creationDate = new Date();
@NotNull
private Role role = Role.MEMBER;
@NotNull
private boolean enabled = true;
private Set<Long> userListIds = new HashSet<Long>();
public void addUserToUserlist(Long newUserId){
userListIds.add(newUserId);
}
public void removeUserFromUserlist(Long newUserId){
userListIds.remove(newUserId);
}
public Set<Long> getUserListIds() {
return userListIds;
}
public void setUserListIds(Set<Long> userListIds) {
this.userListIds = userListIds;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getCreationDate() {
return creationDate;
}
// public void setCreationDate(Date creationDate) {
// this.creationDate = creationDate;
// }
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String name) {
this.firstName = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@Override
public String toString() {
return "User [username=" + username + ", firstName=" + firstName
+ ", lastName=" + lastName + ", email=" + email
+ ", phoneNumber=" + phoneNumber + ", creationDate="
+ creationDate + ", role=" + role + ", enabled=" + enabled
+ "]";
}
}
答案 0 :(得分:0)
我认为这一行:
data:JSON.stringify(userLoginRequest)
应该是
data:encodeURIComponent(JSON.stringify(userLoginRequest))
因为您需要对要在查询字符串中发送的任何数据进行URL编码。 JSONification可能会生成需要在发送之前进行编码的字符,否则服务器可能会遇到解析问题,这就是这种情况。