Table of Contents
Reference of Mock in Dev environment and Release environment
The problem of Mock.JS intercepting HTTP requests
Home Web Front-end JS Tutorial Mock.JS intercepts HTTP request instance analysis

Mock.JS intercepts HTTP request instance analysis

Mar 06, 2018 pm 02:45 PM
http javascript mock.js


MockJS is a relatively common front-end tool for simulating HTTP requests and responses. It can simulate various HTTP requests and return results. Realize the front-end simulation of the back-end interface without a back-end. The basic use of Mock is also relatively simple. In this article, we mainly share with you the analysis of Mock.JS interception of HTTP request examples, hoping to help everyone.

However, when MockJS is introduced,

import Mock from 'mockjs'
Copy after login
Copy after login

will intercept all HTTP requests sent by the front end. Regardless of whether Mock.mock is used to enable Mock simulation, HTTP requests will be intercepted.
This is why, even if Mock.mock is not used, the backend cannot obtain the front-end HTTP request.

Therefore, Once mockjs is referenced, HTTP requests cannot be made through the front end, but will be intercepted by mockjs.

Need to be in npm run build Before, remove the reference to mockjs.

Reference of Mock in Dev environment and Release environment

import global from '../src/common/global';if (global.env === 'dev'){    var Mock = require('mockjs');
}if (global.env === 'dev'){    //Run MOCK 
    for (let mockData of mockDatas){        //console.log(mockData);
        Mock.mock(mockData.url, mockData.data);
    }
}
Copy after login
Copy after login

Only in Dev development environmentglobal.env === 'dev', introduce Mockjs to avoid release version, real HTTP requests are intercepted.

global here is a custom global config variable, not a global variable.

The problem of Mock.JS intercepting HTTP requests

MockJS is a relatively common front-end tool for simulating HTTP requests and responses. It can simulate various HTTP requests and return results. Realize the front-end simulation of the back-end interface without a back-end. The basic use of Mock is also relatively simple: Mock.JS official website
However, when MockJS is introduced,

import Mock from 'mockjs'
Copy after login
Copy after login

will intercept all HTTP requests sent by the front end, regardless of whether Mock.mock is used Enabling Mock simulation will intercept HTTP requests.
This is why, even if Mock.mock is not used, the backend cannot obtain the front-end HTTP request.

Therefore, Once mockjs is referenced, HTTP requests cannot be made through the front end, but will be intercepted by mockjs.

Need to be in npm run build Before, remove the reference to mockjs.

Reference of Mock in Dev environment and Release environment

import global from '../src/common/global';if (global.env === 'dev'){    var Mock = require('mockjs');
}if (global.env === 'dev'){    //Run MOCK 
    for (let mockData of mockDatas){        //console.log(mockData);
        Mock.mock(mockData.url, mockData.data);
    }
}
Copy after login
Copy after login

Only in Dev development environmentglobal.env === 'dev', introduce Mockjs to avoid release version, real HTTP requests are intercepted.

global here is a custom global config variable, not a global variable.

Related recommendations:

Use mock.js to make front-end development and back-end independent

mock.js random data and use express output json interface example tutorial

Tutorial on using Mock.js to intercept AJAX requests in Node.js server environment

The above is the detailed content of Mock.JS intercepts HTTP request instance analysis. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1675
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Understand common application scenarios of web page redirection and understand the HTTP 301 status code Understand common application scenarios of web page redirection and understand the HTTP 301 status code Feb 18, 2024 pm 08:41 PM

Understand the meaning of HTTP 301 status code: common application scenarios of web page redirection. With the rapid development of the Internet, people's requirements for web page interaction are becoming higher and higher. In the field of web design, web page redirection is a common and important technology, implemented through the HTTP 301 status code. This article will explore the meaning of HTTP 301 status code and common application scenarios in web page redirection. HTTP301 status code refers to permanent redirect (PermanentRedirect). When the server receives the client's

HTTP 200 OK: Understand the meaning and purpose of a successful response HTTP 200 OK: Understand the meaning and purpose of a successful response Dec 26, 2023 am 10:25 AM

HTTP Status Code 200: Explore the Meaning and Purpose of Successful Responses HTTP status codes are numeric codes used to indicate the status of a server's response. Among them, status code 200 indicates that the request has been successfully processed by the server. This article will explore the specific meaning and use of HTTP status code 200. First, let us understand the classification of HTTP status codes. Status codes are divided into five categories, namely 1xx, 2xx, 3xx, 4xx and 5xx. Among them, 2xx indicates a successful response. And 200 is the most common status code in 2xx

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to implement HTTP streaming using C++? How to implement HTTP streaming using C++? May 31, 2024 am 11:06 AM

How to implement HTTP streaming in C++? Create an SSL stream socket using Boost.Asio and the asiohttps client library. Connect to the server and send an HTTP request. Receive HTTP response headers and print them. Receives the HTTP response body and prints it.

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

What status code is returned for an HTTP request timeout? What status code is returned for an HTTP request timeout? Feb 18, 2024 pm 01:58 PM

The HTTP request times out, and the server often returns the 504GatewayTimeout status code. This status code indicates that when the server executes a request, it still fails to obtain the resources required for the request or complete the processing of the request after a period of time. It is a status code of the 5xx series, which indicates that the server has encountered a temporary problem or overload, resulting in the inability to correctly handle the client's request. In the HTTP protocol, various status codes have specific meanings and uses, and the 504 status code is used to indicate request timeout issues. in customer

How to solve HTTP 503 error How to solve HTTP 503 error Mar 12, 2024 pm 03:25 PM

Solution: 1. Retry: You can wait for a period of time and try again, or refresh the page; 2. Check the server load: Check the server's CPU, memory and disk usage. If the capacity limit is exceeded, you can try to optimize the server configuration or increase the capacity. Server resources; 3. Check server maintenance and upgrades: You can only wait until the server returns to normal; 4. Check network connection: Make sure the network connection is stable, check whether the network device, firewall or proxy settings are correct; 5. Ensure cache or CDN configuration Correct; 6. Contact the server administrator, etc.

See all articles