簡體   English   中英

使用 angular.js 將數據發送到 php 文件

[英]Using angular.js to send data to a php file

對,所以我有一個角度函數,它使用 http 方法將數據發送到 php 文件。我想處理數據的 php 代碼並將其回顯到頁面上以確認 php 文件已處理它。 我目前收到 undefined 提醒我,我肯定應該把電子郵件的價值還給我嗎?。 謝謝大家

我正在關注本教程https://codeforgeek.com/2014/07/angular-post-request-php/

var request = $http({
 method: "post",
 url: "functions.php",
 data: {
    email: $scope.email,
    pass: $scope.password
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});

//.then occurs once post request has happened
//success callback
request.success(function (response) {
  alert(response.data);
},

//error callback
function errorCallback(response) {
  // $scope.message = "Sorry, something went wrong";
  alert('error');
});

我的php代碼...

//receive data from AJAX request and decode
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);

@$email = $request->email;
@$pass = $request->pass;

echo $email; //this will go back under "data" of angular call.

文檔

$http遺留承諾方法successerror已被棄用。 請改用標准then方法。 如果$httpProvider.useLegacyPromiseExtensions設置為false那么這些方法將拋出$http/legacy錯誤。

您的代碼應如下所示:

request.then(
    function( response ) {
        var data = response.data;
        console.log( data );
    },
    function( response ) {
        alert('error');
    }
);

現在,您需要以 JSON 格式對來自服務器的響應進行編碼,因此替換echo $email; 和:

echo json_encode( array(
    'email' => $email
) );

你可以訪問email從angularjs財產$http.success承諾回調函數(這里面的第一個函數then關閉)由response.data.email

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM