Ajax cross-domain access and its solutions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.