Backbone學習日記[2]:後台(php)接收前端資料並儲存

來源:互聯網
上載者:User

標籤:os   使用   io   java   ar   for   檔案   資料   html   

一、前端定義一個模型,設定一個url,指向提供儲存資料功能的後台檔案,給模型設定一個預設值;執行個體化模型,調用save方法:

<script type="text/javascript" src="jquery.min.full.js"></script><script type="text/javascript" src="Underscore.js"></script><script type="text/javascript" src="Backbone.js"></script><script type="text/javascript">var M = Backbone.Model.extend({      defaults:{          name:‘hello2‘    },    url:"save.php" });  var m = new M;   m.save();   </script>

二、後台php檔案,backbone預設使用jquery的ajax方法,但是它向後台發送的資料是json格式的,所以後台php不能直接通過$_POST("name")來擷取:

<?php      header(‘Content-Type: application/json; charset=utf-8‘);        //不能通過$_POST擷取。因為$_POST[‘paramName‘] 只能接收Content-Type: application/x-www-form-urlencoded提交的資料      $man = json_decode(file_get_contents("php://input"));      file_put_contents("data.txt", $man->name);      echo true;    ?>

啟動並執行結果是把預設資料儲存在一個自動產生個的data.txt檔案中

Backbone學習日記[2]:後台(php)接收前端資料並儲存

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.