国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
php采用ajax數(shù)據(jù)提交post與post常見(jiàn)方法總結(jié),ajaxpost
Home php教程 php手冊(cè) php采用ajax數(shù)據(jù)提交post與post常見(jiàn)方法總結(jié),ajaxpost

php采用ajax數(shù)據(jù)提交post與post常見(jiàn)方法總結(jié),ajaxpost

Jun 13, 2016 am 09:21 AM
ajax php post

php采用ajax數(shù)據(jù)提交post與post常見(jiàn)方法總結(jié),ajaxpost

本文實(shí)例講述了php采用ajax數(shù)據(jù)提交post與post常見(jiàn)方法。分享給大家供大家參考。具體方法如下:

在很多情況下我們使用ajax是不會(huì)有什么問(wèn)題的,但有時(shí)會(huì)碰到ajax數(shù)據(jù)提交post不完整的問(wèn)題,這里舉例給大家分析一下。

下邊是一個(gè)標(biāo)準(zhǔn)的ajax請(qǐng)求代碼,正常情況下是不會(huì)有任何問(wèn)題的,但是,在特定情況下就會(huì)出現(xiàn)問(wèn)題,比如,username=fdas&321的時(shí)候,或者參數(shù)值中出現(xiàn)了&符號(hào),經(jīng)過(guò)了N多遍測(cè)試,發(fā)現(xiàn)數(shù)據(jù)都傳輸了,但是打印出來(lái)數(shù)據(jù)是半截,最后仔細(xì)觀察頭信息發(fā)現(xiàn)傳輸?shù)念^不對(duì),問(wèn)題定位到了js上,發(fā)現(xiàn)字符串拼接的方式會(huì)造成這種問(wèn)題username=fdas&321&password=password這樣就是錯(cuò)誤了的。所以我們需要把傳輸?shù)臄?shù)據(jù)變成 {username:username,passsword:password}這種json格式即可避免問(wèn)題!

示例代碼如下:

復(fù)制代碼 代碼如下:

$(".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(){
}
})
})


補(bǔ)充:四種常見(jiàn)的 POST 提交數(shù)據(jù)方式

① application/x-www-form-urlencoded

這應(yīng)該是最常見(jiàn)的 POST 提交數(shù)據(jù)的方式了。瀏覽器的原生 form 表單,如果不設(shè)置 enctype 屬性,那么最終就會(huì)以 application/x-www-form-urlencoded 方式提交數(shù)據(jù)。請(qǐng)求類似于下面這樣(無(wú)關(guān)的請(qǐng)求頭在本文中都省略掉了):

復(fù)制代碼 代碼如下:

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


首先,Content-Type 被指定為 application/x-www-form-urlencoded;其次,提交的數(shù)據(jù)按照 key1=val1&key2=val2 的方式進(jìn)行編碼,key 和 val 都進(jìn)行了 URL 轉(zhuǎn)碼。大部分服務(wù)端語(yǔ)言都對(duì)這種方式有很好的支持。例如 PHP 中,$_POST['title'] 可以獲取到 title 的值,$_POST['sub'] 可以得到 sub 數(shù)組。

很多時(shí)候,我們用 Ajax 提交數(shù)據(jù)時(shí),也是使用這種方式。例如 JQuery 和 QWrap 的 Ajax,Content-Type 默認(rèn)值都是「application/x-www-form-urlencoded;charset=utf-8」。

② multipart/form-data

這又是一個(gè)常見(jiàn)的 POST 數(shù)據(jù)提交的方式。我們使用表單上傳文件時(shí),必須讓 form 的 enctyped 等于這個(gè)值。直接來(lái)看一個(gè)請(qǐng)求示例:

復(fù)制代碼 代碼如下:

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--

這個(gè)例子稍微復(fù)雜點(diǎn)。首先生成了一個(gè) boundary 用于分割不同的字段,為了避免與正文內(nèi)容重復(fù),boundary 很長(zhǎng)很復(fù)雜。然后 Content-Type 里指明了數(shù)據(jù)是以 mutipart/form-data 來(lái)編碼,本次請(qǐng)求的 boundary 是什么內(nèi)容。消息主體里按照字段個(gè)數(shù)又分為多個(gè)結(jié)構(gòu)類似的部分,每部分都是以 --boundary 開(kāi)始,緊接著內(nèi)容描述信息,然后是回車,最后是字段具體內(nèi)容(文本或二進(jìn)制)。如果傳輸?shù)氖俏募?,還要包含文件名和文件類型信息。消息主體最后以 --boundary-- 標(biāo)示結(jié)束。關(guān)于 mutipart/form-data 的詳細(xì)定義,請(qǐng)前往 rfc1867 查看。

這種方式一般用來(lái)上傳文件,各大服務(wù)端語(yǔ)言對(duì)它也有著良好的支持。
上面提到的這兩種 POST 數(shù)據(jù)的方式,都是瀏覽器原生支持的,而且現(xiàn)階段原生 form 表單也只支持這兩種方式。但是隨著越來(lái)越多的 Web 站點(diǎn),尤其是 WebApp,全部使用 Ajax 進(jìn)行數(shù)據(jù)交互之后,我們完全可以定義新的數(shù)據(jù)提交方式,給開(kāi)發(fā)帶來(lái)更多便利。

③ application/json

application/json 這個(gè) Content-Type 作為響應(yīng)頭大家肯定不陌生。實(shí)際上,現(xiàn)在越來(lái)越多的人把它作為請(qǐng)求頭,用來(lái)告訴服務(wù)端消息主體是序列化后的 JSON 字符串。由于 JSON 規(guī)范的流行,除了低版本 IE 之外的各大瀏覽器都原生支持 JSON.stringify,服務(wù)端語(yǔ)言也都有處理 JSON 的函數(shù),使用 JSON 不會(huì)遇上什么麻煩。

JSON 格式支持比鍵值對(duì)復(fù)雜得多的結(jié)構(gòu)化數(shù)據(jù),這一點(diǎn)也很有用。記得我?guī)啄昵白鲆粋€(gè)項(xiàng)目時(shí),需要提交的數(shù)據(jù)層次非常深,我就是把數(shù)據(jù) JSON 序列化之后來(lái)提交的。不過(guò)當(dāng)時(shí)我是把 JSON 字符串作為 val,仍然放在鍵值對(duì)里,以 x-www-form-urlencoded 方式提交。

Google 的 AngularJS 中的 Ajax 功能,默認(rèn)就是提交 JSON 字符串。例如下面這段代碼:

復(fù)制代碼 代碼如下:

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


最終發(fā)送的請(qǐng)求是:

復(fù)制代碼 代碼如下:

POST http://www.bkjia.com HTTP/1.1
Content-Type: application/json;charset=utf-8
?
{"title":"test","sub":[1,2,3]}


這種方案,可以方便的提交復(fù)雜的結(jié)構(gòu)化數(shù)據(jù),特別適合 RESTful 的接口。各大抓包工具如 Chrome 自帶的開(kāi)發(fā)者工具、Firebug、Fiddler,都會(huì)以樹(shù)形結(jié)構(gòu)展示 JSON 數(shù)據(jù),非常友好。但也有些服務(wù)端語(yǔ)言還沒(méi)有支持這種方式,例如 php 就無(wú)法通過(guò) $_POST 對(duì)象從上面的請(qǐng)求中獲得內(nèi)容。這時(shí)候,需要自己動(dòng)手處理下:在請(qǐng)求頭中 Content-Type 為 application/json 時(shí),從 php://input 里獲得原始輸入流,再 json_decode 成對(duì)象。一些 php 框架已經(jīng)開(kāi)始這么做了。

當(dāng)然 AngularJS 也可以配置為使用 x-www-form-urlencoded 方式提交數(shù)據(jù)。

④ text/xml

之前提到過(guò) XML-RPC(XML Remote Procedure Call)。它是一種使用 HTTP 作為傳輸協(xié)議,XML 作為編碼方式的遠(yuǎn)程調(diào)用規(guī)范。典型的 XML-RPC 請(qǐng)求是這樣的:

復(fù)制代碼 代碼如下:

POST http://www.bkjia.com HTTP/1.1
Content-Type: text/xml
?


examples.getStateName


41
?


XML-RPC 協(xié)議簡(jiǎn)單、功能夠用,各種語(yǔ)言的實(shí)現(xiàn)都有。它的使用也很廣泛,如 WordPress 的 XML-RPC Api,seo/seo.html" target="_blank">搜索引擎的 ping 服務(wù)等等。JavaScript 中,也有現(xiàn)成的庫(kù)支持以這種方式進(jìn)行數(shù)據(jù)交互,能很好的支持已有的 XML-RPC 服務(wù)。不過(guò),我個(gè)人覺(jué)得 XML 結(jié)構(gòu)還是過(guò)于臃腫,一般場(chǎng)景用 JSON 會(huì)更靈活方便。

希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

A Simple Guide to PHP Setup A Simple Guide to PHP Setup Jul 18, 2025 am 04:25 AM

The key to setting up PHP is to clarify the installation method, configure php.ini, connect to the web server and enable necessary extensions. 1. Install PHP: Use apt for Linux, Homebrew for Mac, and XAMPP recommended for Windows; 2. Configure php.ini: Adjust error reports, upload restrictions, etc. and restart the server; 3. Use web server: Apache uses mod_php, Nginx uses PHP-FPM; 4. Install commonly used extensions: such as mysqli, json, mbstring, etc. to support full functions.

Tips for Writing PHP Comments Tips for Writing PHP Comments Jul 18, 2025 am 04:51 AM

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

Learning PHP: A Beginner's Guide Learning PHP: A Beginner's Guide Jul 18, 2025 am 04:54 AM

TolearnPHPeffectively,startbysettingupalocalserverenvironmentusingtoolslikeXAMPPandacodeeditorlikeVSCode.1)InstallXAMPPforApache,MySQL,andPHP.2)Useacodeeditorforsyntaxsupport.3)TestyoursetupwithasimplePHPfile.Next,learnPHPbasicsincludingvariables,ech

Writing Effective PHP Comments Writing Effective PHP Comments Jul 18, 2025 am 04:44 AM

Comments cannot be careless because they want to explain the reasons for the existence of the code rather than the functions, such as compatibility with old interfaces or third-party restrictions, otherwise people who read the code can only rely on guessing. The areas that must be commented include complex conditional judgments, special error handling logic, and temporary bypass restrictions. A more practical way to write comments is to select single-line comments or block comments based on the scene. Use document block comments to explain parameters and return values at the beginning of functions, classes, and files, and keep comments updated. For complex logic, you can add a line to the previous one to summarize the overall intention. At the same time, do not use comments to seal code, but use version control tools.

Mastering PHP Block Comments Mastering PHP Block Comments Jul 18, 2025 am 04:35 AM

PHPblockcommentsareusefulforwritingmulti-lineexplanations,temporarilydisablingcode,andgeneratingdocumentation.Theyshouldnotbenestedorleftunclosed.BlockcommentshelpindocumentingfunctionswithPHPDoc,whichtoolslikePhpStormuseforauto-completionanderrorche

Improving Readability with Comments Improving Readability with Comments Jul 18, 2025 am 04:46 AM

The key to writing good comments is to explain "why" rather than just "what was done" to improve the readability of the code. 1. Comments should explain logical reasons, such as considerations behind value selection or processing; 2. Use paragraph annotations for complex logic to summarize the overall idea of functions or algorithms; 3. Regularly maintain comments to ensure consistency with the code, avoid misleading, and delete outdated content if necessary; 4. Synchronously check comments when reviewing the code, and record public logic through documents to reduce the burden of code comments.

Quick PHP Installation Tutorial Quick PHP Installation Tutorial Jul 18, 2025 am 04:52 AM

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre

Effective PHP Commenting Effective PHP Commenting Jul 18, 2025 am 04:33 AM

The key to writing PHP comments is clear, useful and concise. 1. Comments should explain the intention behind the code rather than just describing the code itself, such as explaining the logical purpose of complex conditional judgments; 2. Add comments to key scenarios such as magic values, old code compatibility, API interfaces, etc. to improve readability; 3. Avoid duplicate code content, keep it concise and specific, and use standard formats such as PHPDoc; 4. Comments should be updated synchronously with the code to ensure accuracy. Good comments should be thought from the perspective of others, reduce the cost of understanding, and become a code understanding navigation device.

See all articles