thinkphp5 + ajax 使用formdata提交數(shù)據(jù)(包括文件上傳) 后臺(tái)返回json完整實(shí)例
本文實(shí)例講述了thinkphp5 + ajax 使用formdata提交數(shù)據(jù)(包括文件上傳) 后臺(tái)返回json。分享給大家供大家參考,具體如下:
知識(shí)點(diǎn)總結(jié)
1.json格式標(biāo)準(zhǔn)
{ 'key':"value" } {"state":"1","msg":"\u7b80\u5386\u6295\u9012\u6210\u529f\uff01"}
前端jquery ajax提交formdata
$.ajax({ })
formdata 獲取表單數(shù)據(jù) 包括文件上傳
HTML
<form class="am-form" id="recruitinfo"> <div class="col-lg6 col-md-6 col-xs-12 m1rem"> <label>姓名</label> <input type="text" name="post[name]" id="name" required > </div> <div class="col-lg6 col-md-6 col-xs-12 m1rem"> <label>手機(jī)號(hào)</label> <input type="text" name="post[iphone]" id="iphone" required> </div> <div class="col-lg10 col-md-10 col-xs-12 m2rem"> <label>附件簡(jiǎn)歷:(您的詳細(xì)信息請(qǐng)都寫(xiě)在簡(jiǎn)歷上,只接受word文檔)</label> <div class="am-form-group am-form-file"> <button type="button" class="am-btn am-btn-danger am-btn-sm"> <i class="am-icon-cloud-upload"></i> 選擇要上傳的簡(jiǎn)歷</button> <input id="doc-form-file" type="file" name="doc" > </div> <div id="file-list"></div> <script> $(function() { $('#doc-form-file').on('change', function() { var fileNames = ''; $.each(this.files, function() { fileNames += '<span class="am-badge">' + this.name + '</span> '; }); $('#file-list').html(fileNames); }); }); </script> <input type="hidden" name="post[jobname]" id="jobname" value="{$data.job}"> <input type="hidden" name="post[jobnameid]" id="jobnameid" value="{$data.id}"> </div> <div class="col-lg-6 col-md-6 col-xs-12"> <button type="submit" class="am-btn am-btn-primary" onclick="submitform()">提交</button> <a href="javascript:window.history.back(-1);" rel="external nofollow" class="am-btn am-btn-default">返回</a> </div> </form>
JS
<script type="text/javascript"> function submitform() { event.preventDefault(); var form =document.getElementById('recruitinfo'), formdata = new FormData(form); var url = '{:url("recruitinfo/postfrom")}'; $.ajax({ url:url, type:'post', data:formdata, dataType:'json', processData:false, contentType:false, success:function (res) { console.log('請(qǐng)求成功!') console.log(res) }, error:function (XMLHttpRequest, textStatus, errorThrown) { console.log('出錯(cuò)啦!') console.log(XMLHttpRequest); console.log(textStatus); console.log(errorThrown); } }) } </script>
php json_encode()函數(shù)轉(zhuǎn)成json
$callbackinfo = array( 'state' => '1', 'msg' => '簡(jiǎn)歷投遞成功!', ); $jsondata =json_encode($callbackinfo); echo $jsondata;
問(wèn)題
1.前端SyntaxError: Unexpected token < in JSON at position 0 報(bào)錯(cuò)
報(bào)錯(cuò)原因
使用的thinkphp5
沒(méi)想到是因?yàn)槭褂昧薲ump()函數(shù) var_dump 這些最后echo出來(lái)的不正確導(dǎo)致的,還要要按標(biāo)準(zhǔn)格式來(lái)啊
解決方法
去掉dump相似的函數(shù)
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門(mén)教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門(mén)教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門(mén)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)(oop)學(xué)習(xí)筆記 (二) - 靜態(tài)變量的屬性和方法及延遲綁定
靜態(tài)變量的類型關(guān)鍵字是static。本文主要是學(xué)習(xí)php中靜態(tài)屬性和靜態(tài)方法的使用方法和基本的示例以及延遲綁定2014-06-06PHP實(shí)現(xiàn)QQ登錄的開(kāi)原理和實(shí)現(xiàn)過(guò)程
本文主要給大家講述了PHP實(shí)現(xiàn)QQ登錄的開(kāi)原理以及相關(guān)的代碼實(shí)現(xiàn)過(guò)程,對(duì)此有興趣和需要的朋友參考下吧。2018-02-02YII Framework的filter過(guò)濾器用法分析
這篇文章主要介紹了YII Framework的filter過(guò)濾器用法,結(jié)合實(shí)例形式分析了filter過(guò)濾器的功能,使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03利用php-cli和任務(wù)計(jì)劃實(shí)現(xiàn)訂單同步功能的方法
下面小編就為大家?guī)?lái)一篇利用php-cli和任務(wù)計(jì)劃實(shí)現(xiàn)訂單同步功能的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05php中使用cookie來(lái)保存用戶登錄信息的實(shí)現(xiàn)代碼
php中使用cookie來(lái)保存用戶登錄信息的實(shí)現(xiàn)代碼,使用php開(kāi)發(fā)的朋友可以參考下2012-03-03CodeIgniter錯(cuò)誤mysql_connect(): No such file or directory解決方法
這篇文章主要介紹了CodeIgniter錯(cuò)誤mysql_connect(): No such file or directory解決方法,需要的朋友可以參考下2014-09-09php 使用html5實(shí)現(xiàn)多文件上傳實(shí)例
在html沒(méi)有出來(lái)之前,要實(shí)現(xiàn)php多文件上傳比較麻煩,需要在form表單里面添加多個(gè)input file域。html5發(fā)布以后,我們可以使用input file的html5屬性multiple來(lái)實(shí)現(xiàn)多文件上傳,需要的朋友可以參考下2016-10-10