javascript - Using JavaEE - SSH framework to develop web projects, how to use CORS to implement cross-domain requests?
伊谢尔伦
伊谢尔伦 2017-06-12 09:23:02
0
1
824

1. I have configured it according to the CORS help document. The document path is: http://www.ruanyifeng.com/blo...;
Local project configuration is as shown in the figure: (1). Import support package
(2) Configuration of web.xml
2. Through the above configuration, in theory it is possible to make cross-domain requests, but in fact it is not possible! Paste my html code:

<!doctype html>
<html>
  <head>
    <title>axios - get example</title>
    <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
  </head>
  <body class="container">
    <h1>axios.get</h1>
    <ul id="people" class="list-unstyled"></ul>

    <script src="axios.min.js"></script>
    <script>
    var url = 'https://api.douban.com/v2/book/1220562';
    
      axios.get(url)
        .then(function (response) {
            document.getElementById('people').innerHTML = '<li class="text-danger">' + response.data + '</li>';
       })
        .catch(function (err) {
          document.getElementById('people').innerHTML = '<li class="text-danger">' + err.message + '</li>';
        });
    </script>
  </body>
</html>

The url is Douban's api
The running result is:
The request header and the request status are:
(1)General
(2)Response
(3)Request
3. I am very confused. The request status code is 200, why is there no The data is returned and the console reports an error? The official document says that as long as it is configured, it can be used across domains. Why can't it be implemented on this machine? Is there something wrong with the configuration? Or is there something missing? Please give me some advice from relevant experts! I am a member of the Third Party of China. I urgently need to solve this problem when I am working on a project. Thank you very much!

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
为情所困

There are many ways to solve cross-domain requests. The cors method relies on the server to return the correct response header information Access-Control-Allow-Origin. Your screenshot shows that the Douban api does not return Access-Control-Allow-Origin:* , so cross-domain failure.
You can also use jsonp to solve cross-domain problems. Douban supports jsonp.


Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template