thinkphp5 + ajax 使用formdata提交數(shù)據(jù)(包括文件上傳) 后臺返回json完整實例
本文實例講述了thinkphp5 + ajax 使用formdata提交數(shù)據(jù)(包括文件上傳) 后臺返回json。分享給大家供大家參考,具體如下:
知識點總結(jié)
1.json格式標準
{ '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>手機號</label> <input type="text" name="post[iphone]" id="iphone" required> </div> <div class="col-lg10 col-md-10 col-xs-12 m2rem"> <label>附件簡歷:(您的詳細信息請都寫在簡歷上,只接受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> 選擇要上傳的簡歷</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('請求成功!') console.log(res) }, error:function (XMLHttpRequest, textStatus, errorThrown) { console.log('出錯啦!') console.log(XMLHttpRequest); console.log(textStatus); console.log(errorThrown); } }) } </script>
php json_encode()函數(shù)轉(zhuǎn)成json
$callbackinfo = array( 'state' => '1', 'msg' => '簡歷投遞成功!', ); $jsondata =json_encode($callbackinfo); echo $jsondata;
問題
1.前端SyntaxError: Unexpected token < in JSON at position 0 報錯
報錯原因
使用的thinkphp5
沒想到是因為使用了dump()函數(shù) var_dump 這些最后echo出來的不正確導致的,還要要按標準格式來啊
解決方法
去掉dump相似的函數(shù)
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP 面向?qū)ο蟪绦蛟O(shè)計(oop)學習筆記 (二) - 靜態(tài)變量的屬性和方法及延遲綁定
靜態(tài)變量的類型關(guān)鍵字是static。本文主要是學習php中靜態(tài)屬性和靜態(tài)方法的使用方法和基本的示例以及延遲綁定2014-06-06PHP實現(xiàn)QQ登錄的開原理和實現(xiàn)過程
本文主要給大家講述了PHP實現(xiàn)QQ登錄的開原理以及相關(guān)的代碼實現(xiàn)過程,對此有興趣和需要的朋友參考下吧。2018-02-02利用php-cli和任務(wù)計劃實現(xiàn)訂單同步功能的方法
下面小編就為大家?guī)硪黄胮hp-cli和任務(wù)計劃實現(xiàn)訂單同步功能的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05php中使用cookie來保存用戶登錄信息的實現(xiàn)代碼
php中使用cookie來保存用戶登錄信息的實現(xiàn)代碼,使用php開發(fā)的朋友可以參考下2012-03-03CodeIgniter錯誤mysql_connect(): No such file or directory解決方法
這篇文章主要介紹了CodeIgniter錯誤mysql_connect(): No such file or directory解決方法,需要的朋友可以參考下2014-09-09