私はログインシステムで作業中です。以前はgetメソッドを使用していました。アプリケーションを実行すると、ajaxリクエストは正しいように見えますが、サーバー側のパラメータはnullですか?Java Spring - RequestMethod.POSTを使用した場合のnull値エラー
古いコード...
- サーバ側
@SuppressWarnings("unchecked")
@RequestMapping(value = "/login", method = RequestMethod.GET)
@CrossOrigin(origins = {"*"})
public ResponseEntity<?> login(
@RequestParam(value="email", required=false, defaultValue="email") String email,
@RequestParam(value="password", required=false, defaultValue="password") String password,
HttpServletRequest request
) throws Exception {
- フロントサイド
export function fetchAuthentication(data) {
let url = 'http://localhost:8080/[email protected]&password=1234';
return function (dispatch) {
axios.get(url)
.then(function (response) {
dispatch(authSuccess(response));
})
.catch(function (error) {
dispatch(authFail(error));
});
}
}
新しいコード..
- サーバ側
@SuppressWarnings("unchecked")
@RequestMapping(value = "/login", method = RequestMethod.POST)
@CrossOrigin(origins = {"*"})
public ResponseEntity<?> login(
@PathVariable(value="email", required=false) String email,
@PathVariable(value="password", required=false) String password,
HttpServletRequest request
) throws Exception {
System.out.println("email email>>>"+email);
- フロントサイド
export function fetchAuthentication(data) {
let url = 'http://localhost:8080/login';
return function (dispatch) {
axios.post(url, data)
.then(function (response) {
if(response.status === "success"){
dispatch(authSuccess(response));
}
else{
// fail - user not found for example
dispatch(authFail(response));
}
})
.catch(function (error) {
dispatch(authFail(error));
});
}
}
'' PathVariable''はあなたのURIに関連していて、POST本体のペイロードには関係しません。 – Matt
それは "RequestParam"でなければなりません - 私はこれを好きでしたが、まだ何も得ていませんでしたか? –
アクシオスコードが正しいようです - 私はポストのヘッダーに "{" email ":" [email protected] "、" password ":" 1234 "}" RequestParamがnullの場合、Java側のログを確認しますか? –