I have simple Login Controller. With in it, I have one action method called signIn(). I am submitting my credential to log into the website through ajax call.
This is my ajax caller function-
var AJAXCaller = function () {
};
AJAXCaller.prototype.call = function (type, uri, header, contentType, content, success, error) {
var config = new SSKSConfig();
var url = config.getLocation() + uri;
return jQuery.ajax({
'type': type,
'url': url,
'headers': header,
'data': JSON.stringify(content),
'success': success,
'error': error
});
};
var caller= new AJAXCaller();
caller.call('POST', '/login', { 'Content-Type': 'application/json' }, 'application/x-www-form-urlencoded', person, fnSuccess, fnError);
where 'person' data like -
{"emailId":"aaa@gmail.com","password":"12345"}
And login controller's signIn() method code is-
@PostMapping(value = "/login",consumes = MediaType.APPLICATION_JSON_VALUE)
public String signIn(@RequestBody Person person) {
if(person.getEmailId().equals("aaa@gmail.com") && person.getPassword().equals("12345")){
return "Success";
}
else{
return "Invalid";
}
}
I have created simple Person POJO class-
public class Person implements PersonSupport, AddressSupport {
private String code;
private String name;
private String emailId;
private String password;
private String contactNo;
private final Address address = new Address();
public Person() {
}
@Override
public void setCode(String code) {
this.code = code;
}
@Override
public String getCode() {
return this.code;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
@Override
public String getContactNo() {
return contactNo;
}
@Override
public void setEmailId(String emailId) {
this.emailId = emailId;
}
@Override
public String getPassword() {
return this.password;
}
@Override
public void setPassword(String password) {
this.password = password;
}
@Override
public String getEmailId() {
return emailId;
}
@Override
public String getRoadNo() {
return address.getRoadNo();
}
@Override
public String getRoadName() {
return address.getRoadName();
}
@Override
public String getCity() {
return address.getCity();
}
@Override
public String getPinCode() {
return address.getPinCode();
}
@Override
public StateList getStateList() {
return address.getStateList();
}
@Override
public void setRoadNo(String roadNo) {
address.setRoadNo(roadNo);
}
@Override
public void setRoadName(String roadName) {
address.setRoadName(roadName);
}
@Override
public void setCity(String city) {
address.setCity(city);
}
@Override
public void setPinCode(String pinCode) {
address.setPinCode(pinCode);
}
@Override
public void setStateList(StateList stateList) {
address.setStateList(stateList);
}
}
Now when i am try to login it gives me 415 Unsupported Media Type error. Can you help me why this error has occurred??? Thanks in advance..
答案 0 :(得分:0)
您需要在jquery ajax调用中设置attr is: thisIsAttribute and the arguments are: element1 element2
。
答案 1 :(得分:0)
使用
@PostMapping(value = "/login",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
由于数据将采用UTF编码,因此您必须使用此值或仅删除消耗部分
答案 2 :(得分:0)
标题和内容类型的混合
$.ajax({
type: type,
url: url,
data: JSON.stringify(content),
contentType: "application/json; charset=utf-8",
dataType: "json",
...
});
您通过了contentType ='application/x-www-form-urlencoded'
。您仍然可以使用contentType变量但传递正确的值。在contentType的情况下无需使用标题。
答案 3 :(得分:0)
从请求映射中删除consumes = MediaType.APPLICATION_JSON_VALUE
,它应该开始工作。
答案 4 :(得分:0)
谢谢。我已经解决了这个问题。我指定了
<mvc:annotation-driven/>
,但我忘了提到将使用哪个HttpMessageConverter
。我需要依赖杰克逊库(jackson-databind)以便我可以将HttpRequestBody
转换为Person
对象,现在,我可以支持MappingJackson2HttpMessageConverter
。