How to fix the failure of ajax on google chrome, ajaxchrome
Problem Analysis: My ajax code is running normally in IE browser, 360 IE core browser, and Firefox, but it is a headache in google chrome, the correct result is always not displayed.
My local development environment is as follows:
Google chrome 28.0.146920.m
Tomcat6.0
The server-side Spring MVC code is as follows:
Controller part:
@RequestMapping(value="/searchRecordBlackListByCardId.json")public String searchRecordBlackListByCardId(HttpServletRequest request,HttpServletResponse response,String cardId,ModelMap mm){response.setContentType("application/json;charset=UTF-8");PwCardSpeciallist pcs = blackListService.getRecordBlackListByCardId(cardId);mm.addAttribute("pwCardSpeciallist", pcs);return "jsonView";}
Xxx-Servlet.XML configuration section:
<bean id="jsonView" class="net.sf.json.spring.web.servlet.view.JsonView"/><bean id="beanNameResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="0" /> </bean><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"p:viewClass="org.springframework.web.servlet.view.JstlView" p:order="100"p:prefix="/WEB-INF/jsp/"p:suffix=".jsp" />
Front-end page:
<script type="text/javascript">$(document).ready(function(){$("#searchBlack").click(function(){$.get('${requestScope.basePath}searchRecordBlackListByCardId.json', { cardId : encodeURI( '123456789' ) }, function (data, textStatus){ alert(data.pwCardSpeciallist.cardId); },'json');return false;})});</script>
For the above problems, let's give three answers so that you can see why ....
First, when using localhost: 8080/cardDemo/searchRecordBlackList.html # address for access:
The second one uses 127.0.0.1: 8080: 8080/cardDemo/searchRecordBlackList.html # For access:
The third one is 192.168.1.100: 8080/cardDemo/searchRecordBlackList.html #
It turns out that google chrome is forbidden to access local ajax resources due to a security policy! This is still a bit pitfall. After all, the company I used to work in claims to be "only supports GOOGLE CHROME" on some projects ".
This story tells us .... in future debugging, try to use the local IP address to access the SERVER, instead of 127.0.0.1 or localhost, so that it will not take several hours like me to look for this error.