从axios使用API​​时访问错误

时间:2019-06-22 22:39:02

标签: elasticsearch vue.js cors axios homestead

我正在尝试从axios使用我自己的API,但是出现访问错误

outputs:1 Access to XMLHttpRequest at VM416:1 Cross-Origin Read Blocking (CORB) blocked cross-origin response http://192.168.10.10:9200/outputs/_search?source=%7B%22query%22:%7B%22match_all%22:%7B%7D%7D%7D&source_content_type=application%2Fjson with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.

我正在使用宅基地。

如果从浏览器访问API,我会得到预期的结果(URL-http://192.168.10.10:9200/outputs/_search?source=%7B%22query%22:%7B%22match_all%22:%7B%7D%7D%7D&source_content_type=application%2Fjson

如果我在错误的页面上显示chrome的标题,这些标题就是这些标题。


Provisional headers are shown
Accept: application/json, text/plain, */*
Origin: http://192.168.10.10
Referer: http://192.168.10.10/outputs
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36

我已在elasticsearch.yml中添加了CORS

Elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, X-User"
http.cors.allow-methords : OPTIONS, HEAD, GET, POST, PUT, DELETE

Request方法是。

Vue.js
<script>
    import axios from 'axios';
    import { loadProgressBar } from 'axios-progress-bar'
    import 'axios-progress-bar/dist/nprogress.css'

    loadProgressBar();


    const query = {
        query: {
            "match_all": {}
        }
    };



    export default {


        data: function() {
            return {
                outputs: []
            }
        },


        mounted() {
            axios.get('http://192.168.10.10:9200/outputs/_search', {
                params: {
                    source: JSON.stringify(query),
                    source_content_type: 'application/json'
                }
            }).then((res) => {
                console.log(res);
            });

            axios.get('/api/output')
                .then((response) => {
                    this.outputs = response.data;
                    console.log('Outputs loaded')
                })
                .catch((err) => {
                    console.log(err);
                });



        },

    }


</script>

期望的回报是API

添加的信息

  • 响应状态代码为200 OK
Request URL: http://192.168.10.10:9200/outputs/_search?source=%7B%22query%22:%7B%22match_all%22:%7B%7D%7D%7D&source_content_type=application%2Fjson
Request Method: GET
Status Code: 200 OK
Remote Address: 192.168.10.10:9200
Referrer Policy: no-referrer-when-downgrade

控制台中的另一个错误。

Access to XMLHttpRequest at 'http://192.168.10.10:9200/outputs/_search?source=%7B%22query%22:%7B%22match_all%22:%7B%7D%7D%7D&source_content_type=application%2Fjson' from origin 'http://192.168.10.10' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
app.js:1455 Uncaught (in promise) Error: Network Error
    at createError (app.js:1455)
    at XMLHttpRequest.handleError (app.js:990)

据我所知,该呼叫没有响应头?

0 个答案:

没有答案