Vue+axios+spring Boot Encounters problems (cross-domain requests)

Source: Internet
Author: User

Tag: Ken Method als encounters version override boot [] Eve

One, click the button will send two requests the question

The first request method is the options

A second request method is a post

The background filter also detects access two times, but it is occasionally repeated access.

This is because a cross-domain request causes an empty request check server to be sent before each request.

You can add this in a background filter:

@Override Public voidDoFilter (ServletRequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {httpservletresponse response1=(httpservletresponse) response; HttpServletRequest Request1=(httpservletrequest) request; Response1.setheader ("Access-control-allow-origin", "*"); Response1.setheader ("Access-control-allow-credentials", "true"); Response1.setheader ("Access-control-allow-methods", "*"); Response1.setheader ("Access-control-allow-headers", "Content-type,access-token"); Response1.setheader ("Access-control-expose-headers", "*"); if (Request1.getmethod (). Equals (RequestMethod.OPTIONS.toString ())) {System.out.println ("-----check------"); Return;    } Chain.dofilter (request, response); }

Second, cross-domain request problem

Add filter, add the above code inside the filter to solve cross-domain request problem

Third, the Axios access interface background read the data are empty the problem is as follows

Front desk:

<script>Exportdefault{data () {return{formItem: {menu_name:‘‘, Menu_info:No, Menu_level:‘‘, Menu_state:true, App_version: [], Operate_user:' Admin ', Menu_superior:‘-‘}, App_versions: [{value:' 100 ', Label:' 1.0.0 '}, {value:' 101 ', Label:' 1.0.1 '}, {value:' 102 ', Label:' 1.0.2 '}, {value:' 110 ', Label:' 1.1.0 '}]}}, methods: {addMenu:function{console.log (form) {json.stringify (form)) This. $http. Post (' Http://localhost:8888/api/manager/addMenu ', { data:JSON.stringify (form) }). Then (function(res) {if(res.data.no = = ' 1 ') {alert (' OK ')            }          })          .Catch(function(Err) {Console.log ('----failed-----');      }); }    }  }</script>

Background log

Can see the front desk clearly passed, backstage can also receive, but why all is null, engaged in a half-day here the problem:

  function {console.log (form) {        json.stringify (form))        this. $http. Post (' http://localhost:8888/api/ Manager/addmenu ', (form))          . Then (function  (res) {            if (res.data.no = = ' 1 ') {              alert (' OK ')            }          })          . Catch (function  (err) {            Console.log ('----failed-----');          });      

In fact, there is no need to transfer JSON, direct object can be, and then look at the background:

That's right.

Vue+axios+spring Boot Encounters problems (cross-domain requests)

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.