[NG] 利用$http.post傳值給php


首先你會有一個data的物件

var data = {
  0:$this.ACADYear,
  1:$this.Semester,
  2:$this.ExamRangeID,
  3:$this.SchoolItemNo,
  4:UpdateID
};



接著你會利用$http.post傳給某個php網頁

$http.post('Example.php', data).
  success(function (data, status, headers, config) {

  }).
  error(function (data, status, headers, config) {
  // called asynchronously if an error occurs
  // or server returns response with an error status.
});


在PHP這邊,參照下列步驟,即可取出資料

$oPostdata = file_get_contents("php://input");
$aRequest = json_decode($oPostdata,true);

$sACADYear = $aRequest[0];
$sSemester = $aRequest[1];
$uExamRangeID = $aRequest[2];
$sSchoolItemNo = $aRequest[3];
$sUpdateID = $aRequest[4];

這樣就可以接到AngularJS傳過來的值嚕

順帶一提,如果傳遞的物件已經有各種屬性值

例如像這樣

var data = {
  ACADYear:$this.ACADYear,
  Semester:$this.Semester,
  ExamRangeID:$this.ExamRangeID,
  SchoolItemNo:$this.SchoolItemNo,
  UpdateID:UpdateID
};

就不需要再json_decode後面加上true,可以直接利用這樣的方式取出資料

$oPostdata = file_get_contents("php://input");
$aRequest = json_decode($oPostdata);

$sACADYear = $aRequest->ACADYear;
$sSemester = $aRequest->Semester;
$uExamRangeID = $aRequest->ExamRangeID;
$sSchoolItemNo = $aRequest->SchoolItemNo;
$sUpdateID = $aRequest->UpdateID;


不過我是不知道file_get_contents的原理,還需要釐清。

留言