1. Problem description
Front end request code
$.ajax({
url: 'getOne',
data: {
name: 'zhangsan',
pwd: '123'
},
type: 'get',
dataType: 'json',
success: function (res) {
alert("成功" + res)
},
error: function (xhr, errorMessage, e) {
alert("失败" + errorMessage);
}
})
Backend servlet code
@WebServlet(name = "getOne", urlPatterns = "/getOne")
public class GetOne extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Set encoding
response.setContentType("application/json;charset=utf-8");
// Processing business logic
// Responding to the request, there may be IO exceptions in the way of using the stream, so the exception is caught
PrintWriter out = response.getWriter();
try {
out.write("ajax request successful");
out.close
} catch (Exception exception) {
out.write(exception);
}
out.close();
}
}
Then it is found that after each processing, it will only respond to the abnormal error function and cannot enter success
2. Problem solving
The code above looks OK at first glance. I thought so at first. However, after some analysis, it is found that the format of the return value type at the back end is incorrect
What do you mean?
I set the JSON
format in the back-end response
response.setContentType("application/json;charset=utf-8");
However, I output the ordinary string
with the stream when responding, not the JSON format string
out.write("ajax request successful");
How to solve it?
Method 1: change the string format to JSON format
back end output: out.Write ("{'data':'ajax request succeeded '}")
front end: alert ("success" + res.data)
method 2: change the type of request and processing to text
back end: response.setcontenttype ("application/JSON; charset = UTF-8")
front end: datatype: 'text'
Read More:
- The solution of calling$. Ajax successfully but the success method does not respond
- Difference between contenttype and datatype in Ajax request of jquery
- Vue a page is mounted to send multiple requests at the same time, and the loading is processed uniformly
- If the request parameter is formdata, use the Ajax operation
- [Solved] ajax Error: Uncaught SyntaxError: Unexpected end of JSON input
- The solution that needs alert to execute after using ajax
- Golang: How to determine structure whether it is empty
- ‘webpack dev server’ is not an internal or external command, nor is it a runnable program or batch file. Solution: error in cnpm run dev:
- How to download files by post submission under Ajax
- [error] IDE service port disabled. To use CLI Call, please enter y to confirm enabling CLI capabilit
- JS to find the last character of the string and remove it
- [Solved] Angular basic create component error: Is it missing an @NgModule annotation
- TypeError: r.indexOf is not a function TypeError: r.indexOf is not a function
- @requestbody: How to Use or Not Use
- How to Fix “HTTP 405 method not allowed” Error
- After Vite starts, it will prompt “network: use ` — host ` to expose”, and the service cannot be accessed through network IP
- Error: Computed property “menuList” was assigned to but it has no setter.
- [Solved] mqtt.js Error: n.createConnection is not a function
- “Failed to load resource: net::ERR_FILE_NOT_FOUND” error. The project created by vue-cli 3.0 can run under dev, and an error is reported after packaging, and the page is blank.