gogoWebsite

No response to POST request occurs when native ajax request

Updated to 2 days ago

When I requested with native ajax, I found == 0. So I searched for a long time and found that the element that was bound to the event was not the element that I clicked. Refer to the following resources

AJAX problem XMLHttpRequest status = 0

Status code

1. If the state is UNSENT or OPENED, return 0. (If the state is UNSENT or OPENED, return 0)
2. If the error flag is set, return 0. (If the error tag is set, return 0)
3. Return the HTTP status code. (Return the HTTP status code)

 

POST request is not responded

The POST request function is as follows:

function post(url, data){
	 var req = createXMLHTTPRequest();
	 if(req){
		 //The post request needs to be added, otherwise an error will be reported
		 ("Content-type","application/x-www-form-urlencoded");
                 ("POST", url, true);
		 //Send a request
		  = function(){
			 //Discern whether the server responds
			 if( == 4 && == 200)
				 requestSuccess();
		 }
		 (data);
	 }
 }

Commenting ("Content-type","application/x-www-form-urlencoded"); can instead request it, but adding this sentence is not possible. I suspect that the <a> tag does not apply to this request header, so I checked the type and description of the content-type:Description of application/x-www-form-urlencoded characters in HTTPStill failed to solve the problem. So I carefully compared the POST request on the Internet and found that I should open first and then setRequestHeader

("POST", url, true);
("Content-type","application/x-www-form-urlencoded");

 

refer to:

Ajax request for native js: At first, I found that the POST request header and open() position are written in reverse

Comparison between native ajax and jQuery ajax

AJAX simple introduction to use

ajax gets the data sent by the servlet

Native js implementation of Ajax

Ajax writing

Description of application/x-www-form-urlencoded characters in HTTP

 

About the error summary:

The post submitted to the servlet is processed by get and post respectively to see if the servlet get method or post method is not set.

2. Is the click event corresponding to the element that binds the click event?

3. Whether there is any problem with setting the POST sequence, open() first, and then set the message header