Home > Web Front-end > uni-app > The uniapp package request cannot be accessed

The uniapp package request cannot be accessed

王林
Release: 2023-05-22 09:21:06
Original
3751 people have browsed it

In recent years, with the popularization of mobile Internet and the continuous advancement of development technology, APP has become more and more popular among people. Among them, uniapp is a cross-platform development framework that can develop APPs for multiple operating systems at the same time and can be directly packaged and deployed. However, when many developers use uniapp for packaging, they will encounter the problem that some requests cannot be accessed. This article will introduce the reasons and solutions for why uniapp packaging requests cannot be accessed from three aspects.

1. CORS Issues

CORS (Cross-Domain Resource Sharing) is a network standard for cross-domain access. Although CORS allows network communication between servers from different origins, it can also cause some security issues. Therefore, browsers generally restrict certain HTTP requests from different origins. This is an important reason why the uniapp package request cannot be accessed.

Solution:

  1. Set the proxy: Add the following code to the project's vue.config.js:
devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:8080',//这里改成你自己的服务器地址
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/',
        },
      },
    },
  },
Copy after login
  1. Use JSONP: JSONP is a solution for cross-domain access. It uses script tags for cross-domain access. However, JSONP is only suitable for GET requests and can only return JSON data.
  2. Set CORS on the server side: If you built your own backend, you can set CORS on the server side to solve this problem.

2. HTTPS issues

When a request is initiated in the APP, if the request address is the http protocol, it will be considered unsafe, and the browser will automatically forward the http request. However, after uniapp is packaged, if you do not change all request addresses to https protocol, then the request may not be accessible.

Solution:

Change all request addresses to https protocol.

3. Network issues

When we use uniapp to package, if the network environment is not good or there is a network failure, the request will not be accessible.

Solution:

  1. Check the network environment: If the network environment is not good, you can move to a place with better signal or use WIFI network.
  2. Check whether the server is normal: If the network failure is caused by server problems, you can contact the server administrator to check.

Summary:

As a cross-platform development framework, uniapp facilitates developers to develop APPs for multiple operating systems at the same time, and can be directly packaged and deployed. When using uniapp for packaging, you encounter the problem that the request cannot be accessed. This article proposes several possible reasons and solutions. In actual development, adjustments need to be made according to specific circumstances to ensure the normal operation of the APP.

The above is the detailed content of The uniapp package request cannot be accessed. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template