PHP uses AJAX data to submit post and post common methods Summary, ajaxpost_php Tutorial

Source: Internet
Author: User

PHP uses AJAX data to submit post and post common methods summary, Ajaxpost


The examples in this paper are the common methods of submitting post and post using AJAX data in PHP. Share to everyone for your reference. Here's how:

In many cases, we use AJAX is not a problem, but sometimes we will encounter the Ajax data submission post is incomplete, here is an example for you to analyze.

Below is a standard AJAX request code, normally there will be no problem, but in a particular case, there will be problems, for example, when the username=fdas&321, or the parameter values appear in the & symbol, after the test of n multiple times, found that the data are transmitted , but print out the data is half, and finally carefully observe the head information to find the transmission of the wrong head, the problem is located on the JS, found that the way the string stitching will cause this problem username=fdas&321&password=password this is wrong. So we need to turn the transmitted data into the JSON format {Username:username,passsword:password} to avoid the problem!

The sample code is as follows:
Copy the code 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 () {
}
})
})
Supplement: Four common POST-submission data methods

①application/x-www-form-urlencoded

This should be the most common way of POST submission data. The native form form of the browser, and if you do not set the Enctype property, the data will eventually be submitted in application/x-www-form-urlencoded manner. The request is similar to the following (extraneous request headers are omitted in this article):

Copy the Code code as follows: POST http://www.bkjia.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, and second, the submitted data is encoded in key1=val1&key2=val2 manner, and both key and Val are URL-transcoded. Most of the service-side languages are very supportive of this approach. For example, in PHP, $_post[' title ' can get to the value of title, $_post[' Sub '] can get a sub array.

Many times, when we use Ajax to submit data, it is also used in this way. For example, the Ajax,content-type default values for JQuery and Qwrap are "application/x-www-form-urlencoded;charset=utf-8".

②multipart/form-data

This is another common way of POST data submission. When we use a form to upload a file, the enctyped of the form must be equal to this value. Look directly at a request example:

Copy the Code code as follows: POST http://www.bkjia.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 slightly more complicated. First, a boundary is created to split the different fields, so the boundary is very long and complex in order to avoid repetition with the body content. Then the content-type indicates that the data is encoded in Mutipart/form-data, and what the boundary of this request is. In the message body, the number of fields is divided into several similar parts, each beginning with--boundary, followed by the content description information, followed by a carriage return, and finally the field specific content (text or binary). If you are transferring files, include the file name and file type information. The message body finally ends with a--boundary--mark. For a detailed definition of mutipart/form-data, go to rfc1867 to view it.

This way is generally used to upload files, the major service-side language for it also has a good support.
The two types of POST data mentioned above are natively supported by the browser, and the native form forms are supported in both ways at this stage. But as more and more Web sites, especially WebApp, use Ajax to interact with the data, we can define new ways of submitting data, which will bring more convenience to development.

③application/json

Application/json this content-type as response head everybody certainly is not unfamiliar. 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 the JSON specification, in addition to the low version of IE, the major browsers are natively supported json.stringify, the service-side language has a function of processing JSON, the use of JSON will not encounter any trouble.

The JSON format supports much more complex structured data than key-value pairs, which is also useful. Remember when I was doing a project a few years ago, the level of data that needed to be submitted was very deep, and I was committing the data JSON serialization later. But at the time I was using the JSON string as Val, still in the key-value pair, to be submitted in a x-www-form-urlencoded manner.

The Ajax feature in Google's AngularJS, by default, is to submit a JSON string. For example, the following code:

The copy Code code is as follows: var data = {' title ': ' Test ', ' Sub ': [N/A]};
$http. Post (URL, data). Success (function (result) {
...
});
The final send request is:
Copy the Code code as follows: POST http://www.bkjia.com http/1.1
Content-type:application/json;charset=utf-8

{"title": "Test", "Sub": [[i]}
This scheme allows for easy submission of complex structured data, especially for RESTful interfaces. The big grab kits, such as Chrome's own developer tools, Firebug, and Fiddler, will show JSON data in a tree-like structure, very friendly. But there are also some service-side languages that do not support this approach, such as PHP's inability to get content from the above request through the $_post object. At this point, you need to do it yourself: When the Content-type is Application/json in the request header, the original input stream is obtained from the php://input and then json_decode into the object. Some PHP frameworks have already begun to do so.

Of course AngularJS can also be configured to submit data using the X-www-form-urlencoded method.

④text/xml

Previously mentioned XML-RPC (XML Remote Procedure call). It is a remote invocation specification that uses HTTP as the transport protocol and XML as the encoding method. The typical XML-RPC request is this:

Copy the Code code as follows: POST http://www.bkjia.com http/1.1
Content-type:text/xml



Examples.getstatename


41



XML-RPC protocol is simple, function enough, all kinds of language implementation have. It is also widely used, such as WordPress xml-rpc api,seo/seo.html "target=" _blank "> Search engine Ping Service and so on. In JavaScript, there are also ready-made libraries to support data interaction in this way and to support existing XML-RPC services. However, I personally think that the XML structure is too bloated, the general scenario with JSON will be more flexible and convenient.

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

http://www.bkjia.com/PHPjc/909351.html www.bkjia.com true http://www.bkjia.com/PHPjc/909351.html techarticle PHP uses AJAX data to submit post and post common methods Summary, Ajaxpost This article describes the PHP using AJAX data to submit post and post common methods. Share to everyone for your reference. Specific Methods ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.