我正在尝试在Web应用程序中创建购物车,该购物车中的重定向是根据添加,更新的产品在spring控制器中完成的。我编写了调用@RequestMapping("/add/{productId}/product")
的axios get请求,该请求重定向到另一个请求映射,并且结果就是回报。我将结果放入地图中,并且可以在控制台上看到地图结果。但是我无法在redux商店中获得结果。
这是重定向链接show?result=updated
。会引发404错误。
这是我的Axiso获取请求。
export const getCart = (pt_id) => async dispatch => {
const res = await axios.get(`http://localhost:8080/cart/add/${pt_id}/product`);
dispatch({
type: GET_CART,
payload: res.data
});
};
在弹簧控制器中
@RequestMapping("/{cartLineId}/update")
public String udpateCartLine(@PathVariable int cartLineId, @RequestParam int count) {
String response = cartService.manageCartLine(cartLineId, count);
return "redirect:/cart/show?"+response;
}
@RequestMapping("/add/{productId}/product")
public String addCartLine(@PathVariable int productId) {
String response = cartService.addCartLine(productId);
return "redirect:/cart/show?"+response;
}
@RequestMapping("/show")
public Map<String, Object> showCart(@RequestParam(name = "result", required = false) String result) {
Map<String, Object> cartMap = new HashMap<String, Object>();
cartMap.put("title", "Shopping Cart");
if(result!=null) {
switch(result) {
case "added":
cartMap.put("message", "Product has been successfully added inside cart!");
cartService.validateCartLine();
break;
case "unavailable":
cartMap.put("message", "Product quantity is not available!");
break;
case "updated":
cartMap.put("message", "Cart has been updated successfully!");
cartService.validateCartLine();
break;
}
}
else {
String response = cartService.validateCartLine();
if(response.equals("result=modified")) {
cartMap.put("message", "One or more items inside cart has been modified!");
}
}
cartMap.put("cartLines", cartService.getCartLines());
return cartMap;
}
如果我打印出cartMap,我可以在控制台上看到结果。
这是网络日志
常规
Request URL: http://localhost:8080/cart/show?result=updated
Request Method: GET
Status Code: 404
Remote Address: [::1]:8080
Referrer Policy: no-referrer-when-downgrade
回复
HTTP/1.1 404
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Access-Control-Allow-Origin: *
Set-Cookie: JSESSIONID=431DE0256413C4B665CFC94601E6DB9C; Path=/; HttpOnly
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/json;charset=UTF-8
Content-Language: en-US
Transfer-Encoding: chunked
Date: Tue, 05 Feb 2019 05:42:22 GMT
请求
GET /cart/show?result=updated HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Accept: application/json, text/plain, */*
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
Referer: http://localhost:3000/cart/add/28/product
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
我在这里做错了什么?