Summary of common methods of php submitting post and post by ajax data

  • 2021-07-26 07:10:24
  • OfStack

This paper illustrates the common method of php submitting post and post by ajax data. Share it for your reference. The specific methods are as follows:

In many cases, we will not have any problems when using ajax, but sometimes we will encounter the problem that ajax data is submitted to post incompletely. Here is an example for everyone to analyze 1.

Below is a standard ajax request code, which will not have any problems under normal circumstances, but will have problems under certain circumstances, for example, username=fdas & 321, or appears in the parameter value & Symbol, after N test for many times, found that the data was transmitted, but the printed data was half. Finally, carefully observe the header information and find that the transmission header is wrong. The problem is located on js, and it is found that the way of string splicing will cause this problem username=fdas & 321 & password=password This is wrong. Therefore, we need to change the transmitted data into the json format {username: username, passsword: password} to avoid problems!

The sample code is as follows:

$(".submit").bind('click',function(){
var username = $("input[name='username']").val();
$.ajax({
url:"post",
type:"post",
dataType:"json",
data:"username="+username+"&password="+password,
timeout:5000,
error:function(){
alert(1)
},
success:function(){
}
})
})

Add: 4 Common POST Data Submission Methods

① application/x-www-form-urlencoded

This should be the most common way for POST to submit data. The browser's native form form, if the enctype property is not set, will eventually submit the data as application/x-www-form-urlencoded. The request is similar to the following (irrelevant request headers have been omitted in this article):

POST https://www.ofstack.com HTTP/1.1
Content-Type: application/x-www-form-urlencoded;charset=utf-8
 
title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3

First, Content-Type is designated as application/x-www-form-urlencoded; Secondly, the submitted data is based on key1=val1 & key2=val2 is encoded, and URL transcoding is carried out for key and val. Most server-side languages have good support for this approach. For example, in PHP, $_ POST ['title'] can get the value of title, and $_ POST ['sub'] can get the sub array.

Many times, when we submit data with Ajax, we also use this method. For example, the default values of Ajax and Content-Type of JQuery and QWrap are "application/x-www-form-urlencoded;; charset=utf-8 ".

② multipart/form-data

This is another common POST data submission method. When we upload files using forms, we must have enctyped of form equal to this value. Let's look directly at a request example:

POST https://www.ofstack.com HTTP/1.1
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA
 
------WebKitFormBoundaryrGKCBY7qhFd3TrwA
Content-Disposition: form-data; name="text"
 
title
------WebKitFormBoundaryrGKCBY7qhFd3TrwA
Content-Disposition: form-data; name="file"; filename="chrome.png"
Content-Type: image/png
 
PNG ... content of chrome.png ...
------WebKitFormBoundaryrGKCBY7qhFd3TrwA--

This example is a little more complicated. Firstly, an boundary is generated to divide different fields. In order to avoid duplication with the text content, boundary is very long and complicated. Then it is indicated in Content-Type that the data is encoded in mutipart/form-data, and what is the content of boundary in this request. The message body is divided into several parts with similar structure according to the number of fields. Each part starts with--boundary, followed by content description information, then carriage return, and finally the specific content of the field (text or binary). If the transfer is a file, also include the file name and file type information. The message body ends with--boundary. For a detailed definition of mutipart/form-data, go to rfc1867.

This method is generally used to upload files, and it is also well supported by major server languages.
The two modes of POST data mentioned above are supported natively by browsers, and only these two modes are supported by native form forms at this stage. However, with more and more Web sites, especially WebApp, all use Ajax for data interaction, we can completely define new data submission methods, which brings more convenience to development.

③ application/json

application/json, Content-Type, is no stranger to everyone as the response header. In fact, more and more people now use it as a request header to tell the server that the message body is a serialized JSON string. Due to the popularity of JSON specification, all major browsers except the lower version of IE support JSON. stringify natively, and server-side languages also have functions to deal with JSON, so there will be no trouble in using JSON.

The JSON format supports much more complex structured data than key-value pairs, which is also useful. I remember when I was doing a project a few years ago, the data level that needed to be submitted was very deep, and I submitted it after serializing the data JSON. But at that time, I put the JSON string as val, still in the key-value pair, and submitted it as x-www-form-urlencoded.

The Ajax function in AngularJS of Google commits the JSON string by default. For example, the following code:

var data = {'title':'test', 'sub' : [1,2,3]};
$http.post(url, data).success(function(result) {
...
});

The final request sent is:
POST https://www.ofstack.com HTTP/1.1
Content-Type: application/json;charset=utf-8
 
{"title":"test","sub":[1,2,3]}

This scheme can easily submit complex structured data, especially suitable for RESTful interface. All major package grabbing tools, such as Chrome's own developer tools, Firebug and Fiddler, will display JSON data in a tree structure, which is very friendly. However, some server-side languages do not yet support this approach. For example, php cannot get content from the above request through the $_POST object. At this time, you need to do it yourself: when Content-Type is application/json in the request header, get the original input stream from php://input, and then json_decode becomes an object. 1 Some php frameworks have already started to do this.

Of course, AngularJS can also be configured to submit data using x-www-form-urlencoded.

④ text/xml

XML-RPC (XML Remote Procedure Call) was mentioned earlier. It is a remote call specification that uses HTTP as the transport protocol and XML as the encoding mode. A typical XML-RPC request looks like this:

POST https://www.ofstack.com HTTP/1.1
Content-Type: text/xml
 
<!--?xml version="1.0"?-->
<methodcall>
<methodname>examples.getStateName</methodname>
<params>
<param>
<value><i4>41</i4></value>
 
</params>
</methodcall>

XML-RPC protocol is simple, functional enough, all kinds of language implementation. It is also widely used, such as XML-RPC Api, seo/seo. html "target=" _ blank " > ping service of search engine and so on. In JavaScript, there is also a ready-made library to support data interaction in this way, which can well support the existing XML-RPC services. However, I personally think that the structure of XML is still too bloated, and it will be more flexible and convenient to use JSON in a general scene.

I hope this article is helpful to everyone's PHP programming.


Related articles: