当输入浏览器的网址返回预期数据时,fetch()返回不透明

时间:2018-07-28 22:29:08

标签: javascript cors fetch

我有一个react应用,可对我编写的WebAPI进行提取。这是WebAPI的网址:

http://ec2-54-70-8-113.us-west-2.compute.amazonaws.com/api/homepage

当我在浏览器中输入webAPI的网址时,我会得到以下提示:

<ArrayOfHomepage xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebAPI">
    <Homepage>
        <product_description>
            This application will allow the user to use his/her device camera to scan a product barcode and look up the value of the prodcut on Amazon and eBay based on the products barcode
        </product_description>
        <product_title>What's my crap worth</product_title>
        <return_code>1 - successful read from database</return_code>
    </Homepage>
</ArrayOfHomepage>

这正是我期望收到的。这是我的React Fetch()的代码:

let url = "http://ec2-54-70-8-113.us-west-2.compute.amazonaws.com/api/homepage";

fetch(url, {mode: "no-cors"})
  .then(response => response)
  .then(data => console.log("data: ", data)); 

这就是返回的内容:

Data:
Response {type: "opaque", url: "", redirected: false, status: 0, ok: false, …}

不是我所期望的。当我从提取中删除{mode:“ no-cors”}参数时,出现此错误:

No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:8080' is therefore not allowed access. 
If an opaque response serves your needs, set the request's mode to 'no-cors'
to fetch the resource with CORS disabled.

关于如何通过访存正确检索数据的任何建议?

感谢您的任何建议。

0 个答案:

没有答案