[英]XMLHttpRequest post 400 Bad request error when calling wcf Service
[英]XMLHttpRequest Returning Bad Request (400) while calling from java script , if i am calling from Java or Postman client it working fine
我正在嘗試使用 java 腳本調用 Spring rest 方法。 我正進入(狀態:
POST http://www.davco.com/search/loginAuthentication 400(錯誤請求)
下面是我的 Java 腳本代碼,用於調用我擁有的 Spring REST 服務
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);
當我嘗試從 Google Postman 客戶端調用相同的方法時,我能夠點擊服務並獲得響應。
當我從 Javascrpt 調用它時,我無法理解我在做什么錯。 我參考了這個鏈接,這個鏈接也是如此。
我嘗試了不同的內容類型header,例如:
http.setRequestHeader("內容類型", "text/html"); http.setRequestHeader("內容類型", "application/json"); http.setRequestHeader("內容類型", "Accept=application/xml");
我從瀏覽器請求中看到,POST 調用的請求負載如下所示,這是正確的:
下面是我的 Spring Rest 服務碼:
@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(".");
....
我只是想知道為什么它工作並且我能夠從 PostMan 客戶端調用但在我嘗試從 java 腳本調用時不工作? 正如您在下面的屏幕截圖中看到的那樣。
提前感謝您提供任何線索或信息來擺脫這種情況。 我花了將近一天的時間,但想不通。
試試吧:
var params = {
eid:userEmailId,
pwd:userPwd
};
http.send(params);
要么:
var formData = new FormData();
formData.append("eid", userEmailId);
formData.append("pwd", userPwd);
http.send(formData);
花了很多時間后,我仍然無法弄清楚上述實現中到底存在什么問題。
但是現在我像這樣更改並可以使用以下樣式正常工作:
var response = $.post("/search/loginAuthentication",{"eid":userEmailId, "pwd" :userPwd},
function(data,status){
processAuthenticationResponse(data);
},'json');
}
我有同樣的問題。 但后來我發送了 JSON 的字符串而不是 JSON object。然后它起作用了。
request.send(JSON.stringify(json));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.