springboot ajax request to controller failed

Problem Description

The springboot ajax request to the controller fails
and always reports an error in the url

solution:

Obtain error information by capturing error events
1. The first parameter XMLHttpRequest returned by error events:
XMLHttpRequest.readyState: meaning of status code
0 -- (uninitialized) has not called send() method
1 -- (loading) The send() method has been called and the request is being sent
2 - (loading complete) the send() method has been executed and all response content has been received
3 - (interactive) parsing the response content
4 - (complete) the response content parsing is complete, you can Called on the client side,
you can check where your request goes wrong

(readyState object status value, 0—uninitialized 1—loading 2—loaded 3—interaction 4—completed.
responseText The string form of data returned from the server process.
status The numeric code returned from the server, such as the common 404 (not found) and 200 (ready)
onreadystatechange event handler for events triggered each time the state changes.
status Text string information accompanying the status code)

2. If the status is obtained or the console displays:
XMLHttpRequest.status: 200
XMLHttpRequest.readyState: 4

 error: function (XMLHttpRequest, textStatus, errorThrown) {
              alert(XMLHttpRequest.readyState);//当数据请求失败可以查看请求的状态

The reason for this situation is as follows:
the format of the returned content is inconsistent with that declared in the ajax request.
dataType: "json", whether the front and back data types are consistent.
As in the example below, the dataType is txt, but the return is json, and the background returns String type data. When the front-end ajax receives data, the dataType needs to be defined as text. Or convert the data into json format in the background.
3. Check whether the url is wrong

Guess you like

Origin blog.csdn.net/sunxiaohong__/article/details/125404328