How does $.post () request to send data?

1, we know that the general form request is this purple

//
<form method="post" action="php.php"> 
    <input type="text" name="username" />
</form>

//
//php.php
$username = $_POST["username"]

2. The native ajax request for the xhr object looks like this:

var xmlhttp = new XMLHttpRequst();
xmlhttp.open("POST","/statics/demosource/demo_post2.php",true);

//name
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

xmlhttp.send("fname=Henry&lname=Ford");

Native ajax post request, is still a mock form, and the backend still obtains the value through the name field of the form

3, HTTP request implemented by $.post ():

//
$.post("web.php", { "username": "lofayo" }, function(data) {
    console.log(data)
})

//
//web.php
$username = $_POST["username"]

in fact, I don"t quite understand this. The most original HTTP requests are implemented through forms, and the first three methods are also implemented by simulating form requests, but this kind of release is not a simulated form, but it can still realize HTTP requests. And no matter how the front end requests, the back end obtains the sent data in a unified way

.

what is the specific principle of the HTTP request of this $.post () method? Is it really another way to request without imitating a form?

Mar.11,2021

first 2 is not a mock form, the form should be form-data , and 2 is application/x-www-form-urlencoded .
secondly, if you encapsulate the code in 2 , isn't that 3 ?
when in fact 1 2 only does one thing, which is to satisfy the http protocol, and then send it.

Menu