现象
DevTool的 Console报错:
VM249 jquery.js:8241 XHR failed loading: POST "http://localhost:8093/api_xxxx".
send | @ | VM249 jquery.js:8241 | |
ajax | @ | VM249 jquery.js:7720 | |
jQuery.(anonymous function) | @ | VM249 jquery.js:7246 | |
calcProductRate | @ | VM252 api.js:15 | |
onclick | @ | api:119 |
api.js:15
$.post(url, args, function(data) {
DevTool的 Network的报错
XHR 请求的资源 报红色, status (canceled)
分析
按钮的type写成了submit
<input type="submit" value="查询">
The event successfully sends the request, but is is canceled then (but processed by the server). The reason is, the elements submit forms on click events, no matter if you make any ajax requests on the same click event.
解决
1. To prevent request from being cancelled, JavaScript event.preventDefault(); have to be called:
<script>
$("#call_ajax").on("click", function(event){
event.preventDefault();
$.ajax({
...
});
});
</script>
2. 改成button
<input type="button" value="查询">