英文:
React fetch works in one app, "Failed to Fetch" in another
问题
我有一个样板的Visual Studio React JS应用程序。这是从VS向导中获取的,它会为你提供ASP.NET的“天气预报”控制器。自然地,客户端的React JS应用程序调用获取数据的功能在没有问题的情况下工作正常。
我还有一个完全独立的、功能完整的独立React JS应用程序,我想要将其调整为调用这个服务器。所以我将从MS生成的应用程序中获取数据的小代码复制到了独立的应用程序中:
fetch("https://localhost:44439/weatherforecast");
然后我在Visual Studio Code中调试了独立的应用程序。
这次获取确实到达了我从MS生成的“天气预报”控制器,就像在MS生成的应用程序中一样。我的控制器返回了与其他应用程序相同的预先设定的数据。
但是在我的独立应用程序中,我收到了一个错误:
未处理的运行时错误
TypeError: 获取数据失败
为什么完全相同的调用在一个应用程序中工作,而在另一个应用程序中却不工作,尽管它实际上已经到达了服务器?我如何诊断出错的原因?这个错误信息并不是很有帮助。
英文:
I've got a boiler-plate Visual Studio React JS app. The one that comes from the VS wizard and gives you the ASP.NET "Weather Forecast" Controller. Naturally the client React JS app's call to fetch works just fine with no issues.
I also have a completely separate, functional standalone React JS app that I want to adapt to call this server. So I took my little fetch call from the MS-generated app and copied it into the standalone app
fetch("https://localhost:44439/weatherforecast");
Then I debugged the standalone app in Visual Studio Code.
The fetch does reach my MS generated "Weather Forecast" controller, just as it does with the MS-generated app. And my controller returns the same canned data, just as it does with the other app.
But then in my standalone app, I get an error
Unhandled Runtime Error
TypeError: Failed to fetch
Why would the exact same call work in one app and not in another when it actually reaches the server? And how do I diagnose what's going wrong? This isn't a very helpful error message.
答案1
得分: -3
错误消息 "TypeError: Failed to fetch" 通常表示您独立的 React 应用程序在尝试从服务器获取数据时遇到了问题。由于请求已达到服务器,并且服务器以正确的数据响应了您的 "Weather Forecast" 控制器,所以问题可能与您独立 React 应用程序中的客户端代码有关。
以下是一些可能导致一个应用中的 fetch 调用工作正常而另一个应用中不工作的常见原因:
-
CORS(跨源资源共享)问题:CORS 限制可能会阻止独立应用程序访问服务器数据。浏览器执行 CORS 以防止可能恶意的跨源请求。生成的 MS 应用程序和独立应用程序可能对 CORS 有不同的配置。确保您的服务器允许来自独立应用程序托管的域或来源的请求。
-
HTTPS 问题:浏览器对从使用 HTTPS 提供的页面到使用 HTTP 提供的页面发出的请求执行更严格的安全策略。确保您的客户端应用程序和服务器都使用 HTTPS 或 HTTP。如果您的独立应用程序使用 HTTPS 提供并且您的服务器使用 HTTP 提供,由于混合内容限制,fetch 可能会失败。
-
防火墙或代理设置:独立应用程序正在执行的网络环境可能具有阻止请求的防火墙或代理设置。
要诊断问题并获取有关错误的更多信息,您可以采取以下步骤:
-
检查网络选项卡:打开浏览器的开发者工具(通常可通过 F12 访问),转到 "网络" 选项卡,并从您的独立应用程序重新进行 fetch 请求。在 "网络" 选项卡中检查是否有关于失败请求的任何错误或附加信息。
-
捕获 Fetch 错误:在进行 fetch 调用时,您可以添加一个
.catch()
块来捕获任何错误并将其记录到控制台。这样,您可能会获取有关出现问题的更多信息。
fetch("https://localhost:44439/weatherforecast")
.then(response => response.json())
.then(data => {
// 处理数据
})
.catch(error => console.error('Fetch error:', error));
-
检查服务器响应:确保服务器的响应是预期的格式,没有抛出任何意外的错误。例如,如果响应不是有效的 JSON 格式,则
response.json()
方法将失败。 -
查看服务器端日志:检查服务器端日志,查看与传入请求相关的任何潜在错误或警告。
-
查看服务器端 CORS 配置:查看服务器端的 CORS 配置,并确保它允许来自独立应用程序域或来源的请求。
通过按照这些步骤,您应该能够收集有关在您的独立 React 应用程序中的 fetch 调用中可能导致问题的原因的更多信息。
英文:
The error message "TypeError: Failed to fetch" typically indicates that the fetch request made by your standalone React app encountered an issue while trying to fetch data from the server. Since the request is reaching the server and the server is responding with the correct data for your "Weather Forecast" controller, the issue is likely related to the client-side code in your standalone React app.
Here are some common reasons why the fetch call might work in one app and not in another:
-
CORS (Cross-Origin Resource Sharing) Issue: CORS restrictions may prevent the standalone app from accessing data from the server. Browsers enforce CORS to prevent cross-origin requests that could be malicious. The MS-generated app and the standalone app might have different configurations for CORS. Ensure that your server allows requests from the domain or origin where your standalone app is hosted.
-
HTTPS Issue: Browsers enforce stricter security policies for requests made from a page served over HTTPS to a page served over HTTP. Make sure that both your client app and server are either using HTTPS or HTTP. If your standalone app is served over HTTPS and your server is served over HTTP, the fetch might fail due to mixed content restrictions.
-
Firewall or Proxy Settings: The network environment where the standalone app is being executed might have firewall or proxy settings that are blocking the request.
To diagnose the issue and get more information about the error, you can take the following steps:
-
Check the Network Tab: Open the browser's Developer Tools (usually accessible via F12), go to the "Network" tab, and make the fetch request again from your standalone app. Check if there are any errors or additional information about the failed request in the "Network" tab.
-
Catch Fetch Errors: When making the fetch call, you can add a
.catch()
block to catch any errors and log them to the console. This way, you might get more information about what's going wrong.
fetch("https://localhost:44439/weatherforecast")
.then(response => response.json())
.then(data => {
// Handle the data
})
.catch(error => console.error('Fetch error:', error));
-
Inspect the Server Response: Ensure that the response from the server is in the expected format and is not throwing any unexpected errors. For example, if the response is not in valid JSON format, the
response.json()
method will fail. -
Review Server-side Logs: Check the server-side logs for any potential errors or warnings related to the incoming requests.
-
Review Server-side CORS Configuration: Review the CORS configuration on the server-side and ensure that it allows requests from the standalone app's domain or origin.
By following these steps, you should be able to gather more information about what might be causing the issue with the fetch call in your standalone React app.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论