'XMLHttpRequest Returning Bad Request (400) while calling from java script , if i am calling from Java or Postman client it working fine
I am trying to call Spring rest method using java script. I am getting:
POST http://www.davco.com/search/loginAuthentication 400 (Bad Request)
Below is my Java scrpt Code to invoke Spring REST service that I have
var userEmailId= $("#emailAddress").val();
var userPwd= $("#userPassword").val();
if (empty(userPwd)) {
alert("Please enter password");
return false;
}
var http = new createXMLHttpRequest();
var url = "/search/loginAuthentication";
var params = 'eid=' +userEmailId+'&pwd='+userPwd
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/xml");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
When i am trying to call same method from Google Postman client I am able to hit services and get the response..
I am not able to understand what wrong i am doing while i am calling it form Javascrpt. I have refer this link, this link as well.
I tried different Content-type header like :
http.setRequestHeader("Content-type", "text/html"); http.setRequestHeader("Content-type", "application/json"); http.setRequestHeader("Content-type", "Accept=application/xml");
I am seeing from browser request and request payload for POST call is going like below that is correct :
Below is my Spring Rest Services Code:
@RequestMapping(method = RequestMethod.POST, value = "/loginAuthentication" ,headers = "Accept=application/xml, application/json, text/html")
public @ResponseBody String loginAuthnticationForHTEtb(@RequestParam("eid") String userEmailId,@RequestParam("pwd") String password
,HttpServletRequest request, HttpServletResponse response) {
StringBuffer result = new StringBuffer();
try {
String domainVraiable=Context.getDomainName();
int inxexOfDot=domainVraiable.indexOf(".")+1;
int lastIndexOf=domainVraiable.lastIndexOf(".");
....
I am just wondering why it working and i am able to call from PostMan Client but not working while i am trying to call from java script? As you can see in below screenshot.
Thanks in advance for any kind of clue or information to get rid of this. I spend almost a day but could not figured out.
Solution 1:[1]
try it:
var params = {
eid:userEmailId,
pwd:userPwd
};
http.send(params);
or:
var formData = new FormData();
formData.append("eid", userEmailId);
formData.append("pwd", userPwd);
http.send(formData);
Solution 2:[2]
After spending a lot of time,still I am not able to figure out what exactly the problem in above implementation.
But now I change like this and working fine with below style :
var response = $.post("/search/loginAuthentication",{"eid":userEmailId, "pwd" :userPwd},
function(data,status){
processAuthenticationResponse(data);
},'json');
}
Solution 3:[3]
I had same problem. But then I sent string of JSON instead of JSON object. Then it worked.
request.send(JSON.stringify(json));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Gautam |
| Solution 3 | Harshad Karanjule |


