When you request another Web site in an AJAX way, cross-domain request issues occur.
XMLHttpRequest cannot load http://google.com/. ' access-control-allow-origin' Http://run.jsbin.io ' is therefore not allowed Access.
Compile a server-side servlet test
@RequestMapping ("/haha") @ResponseBody string haha (string haha, httpservletrequest req, HttpServletResponse resp) { //resp.addheader ("Access-control-allow-origin", "null"); System.out.println (haha); System.out.println ("You accessed!!!" ); return haha + ":" + Req.getmethod (); }
I'm using spring mvc, and if requestmapping does not have parameters, the default is '/' and if the request is not found, go to it.
No parameter requestmapping only allow one, otherwise cannot be deployed, error.
In spring MVC, if the request method is not specified, the default is yes, either get or post can find the resource, as long as the URL is correct.
below request this servlet with Ajax
<Body> <H1ID= ' resp '></H1> <inputID= ' haha 'type= ' text '></input> </Body><Scripttype= "Text/javascript"> $('#haha'). KeyDown (function(event) {Console.log (Event.which)if(Event.which== -) {$.get ('Http://localhost:8080/news/haha',{'haha':'WYF'},function(data) {Console.log (data) Console.log ('get over') }) } });</Script>
Server-Side OUTPUT "You succeed!!!", which indicates that the cross-domain request server is a concern, the problem is that the server is forbidden to return or the browser does not allow users to view the returned content.
As far as I'm guessing, it's probably the latter, that is, the chrome analysis returns the results and discovers the cross-domain, so that the user does not see.
The solution is simple: make a form to commit, because only Ajax has cross-domain access issues, and the submission form is not the same as Ajax.
After the form is submitted, the server determines the browser-side page jump, the power is completely given to the server, and Ajax just through the server to obtain data
The following forms can be submitted for successful
<formAction= ' Http://localhost:8080/news/haha 'Method= "GET"> <inputID= ' haha 'type= ' text '></input> <inputtype= "Submit"></input> </form>
But filling out the form is too cumbersome, so you can make a virtual form and submit it
$ (document). Ready (function() { var form=$ (' <form></form> ') $ ( form). attr ({"action": ' Http://localhost:8080/news/haha ', "method": ' Post '}). Append ("<input name= ' haha ' value= ' haha weidiao ' > ') $ (form). Submit () console.log (' haha ') })
Ajax cross-domain access and its solutions