Mock. Js simulates Ajax data, intercepts Ajax requests, returns mock data, and tests the mockJS front end without the need for a back-end return

	

 Copy the code

Before the Ajax request, mack defines the returned data

Mock. Mock ('http://laoyu', {" errorCode ": 0,//0 indicates success, 1 indicates error "message": "xx message is not complete ", // error message is displayed});Copy the code

In Ajax, the URL for open() is the same as in mock, as in my casehttp://laoyu, then

XHR. Open (" post/get ", "http://laoyu", true/false)Copy the code

All right, with that said, let’s test it out

  Copy the code

See, the gleason’s message returns the data we used to mock, so that we can run our own tests without the behind-the-scenes

Xhr. readyState five states

00 - (uninitialized) Open () method not called 1 - (server connection established) Open () method called, sending request 2 - (request received) Send () method completed, All response contents have been received 3 - (request processing) Parsing response contents 4 - (request completed) Parsing response contents is complete and ready to be invoked on the clientCopy the code

The article directories