欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

thinkphp5 + ajax 使用formdata提交數(shù)據(jù)(包括文件上傳) 后臺(tái)返回json完整實(shí)例

 更新時(shí)間:2020年03月02日 12:45:40   作者:星耀學(xué)園  
這篇文章主要介紹了thinkphp5 + ajax 使用formdata提交數(shù)據(jù)(包括文件上傳) 后臺(tái)返回json操作,結(jié)合實(shí)例形式分析了thinkphp5 + ajax 使用formdata提交數(shù)據(jù)、文件上傳與后臺(tái)返回json遇到的相關(guān)問(wèn)題即解決方法,需要的朋友可以參考下

本文實(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)文章

最新評(píng)論