ionic2/angular2 不能訪問laravel5.3 建立的api

來源:互聯網
上載者:User

laravel 5.3 api已經配置完成了

    //category.service.ts    private headers = new Headers({'Content-Type': 'application/json','Accept':'application/json','Authorization':'Bearer '+window.localStorage.getItem('token')});    private heroesUrl = 'http://xingao.5188cms.com/api/user';  // URL to web api    private handleError(error: any): Promise {      console.error('An error occurred', error); // for demo purposes only      return Promise.reject(error.message || error);    }

這樣去訪問,提示XMLHttpRequest cannot load http://xingao.5188cms.com/api/user. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.,看樣子是跨域的問題,可是我用postman是可以成功擷取到資料的

第二步

我把連結換成了一個http://xingao.5188cms.com/test.php

//test.php

還是不行XMLHttpRequest cannot load http://xingao.5188cms.com/test.php. Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.

第三步

我把連結換成了一個/test.json,卻可以的

那到底是什麼原因。。。。

回複內容:

laravel 5.3 api已經配置完成了

    //category.service.ts    private headers = new Headers({'Content-Type': 'application/json','Accept':'application/json','Authorization':'Bearer '+window.localStorage.getItem('token')});    private heroesUrl = 'http://xingao.5188cms.com/api/user';  // URL to web api    private handleError(error: any): Promise {      console.error('An error occurred', error); // for demo purposes only      return Promise.reject(error.message || error);    }

這樣去訪問,提示XMLHttpRequest cannot load http://xingao.5188cms.com/api/user. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.,看樣子是跨域的問題,可是我用postman是可以成功擷取到資料的

第二步

我把連結換成了一個http://xingao.5188cms.com/test.php

//test.php

還是不行XMLHttpRequest cannot load http://xingao.5188cms.com/test.php. Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.

第三步

我把連結換成了一個/test.json,卻可以的

那到底是什麼原因。。。。

Access-Control-Allow-Origin
跨域的問題
這是瀏覽器的限制

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.