コントローラにおける関連部分:Ajax呼び出し
@RequestMapping(value= "/makeTransfer", method = RequestMethod.POST)
public @ResponseBody String makeTransfer(Map<String, Object> model, @RequestParam int id, @RequestParam int sum, @RequestParam int ownerAccount, @RequestParam int ownerBalance, @RequestParam int receiverAccount){ ...}
Ajax呼び出し私はonclickを使う私のJSPで
function transfer(obj, idd, ownerAcc, ownerbal){
var receiverAccount = parseInt($(obj).parent().find('#recAcc_field').val());
var sum = parseInt($(obj).parent().find('#sum_field').val());
var updatedSum = ownerbalance - sum;
var id = parseInt(idd);
var ownerAccount = parseInt(ownerAcc);
var ownerbalance = parseInt(ownerbal);
alert(ownerbalance);
$.ajax({
type : "POST",
url : "makeTransfer.html",
data: ({id : id, sum : sum, ownerAccount : ownerAccount, ownerbalance : ownerbalance, receiverAccount : receiverAccount}),
success : function(data) {
if (data=="success") {$(obj).closest('tr').find('#accBalance').val(updatedSum)}
else {alert("error!!")}
}
})
}
(私はすべての整数を持ってしようとしています)その関数を呼び出す:
Receiving Account: <input id="recAcc_field" type="text">
Sum: <input id = "sum_field" type="text" onblur="checkTransfer(this,${account.balance})">
<input id="transferButton" disabled type="submit" value="Transfer" onclick='transfer(this, ${personna.id}, ${account.accountId}, ${account.balance})'>
私のコントローラには他のAjax呼び出しがあり、すべてがうまく動作します。このコードse前に書かれたコードと非常によく似ています。私のjavascript関数のすべてのパラメータは整数でなければなりませんが、私はそれをとにかく "parseInt"します。 .val()は何を返しますか?整数か文字列か(JavaScriptはJavaに似ていませんが、不思議です)。私は取得しています エラーは次のとおりです。Failed to load resource: the server responded with a status of 400 (Bad Request)
あなたは、正確なPOSTリクエストを見ることができるようにするには、Chromeデベロッパーツールになります
ありがとうございました@david! :) –