私はシーンに不慣れで、Angular.js を使用して HTTP POST 要求を作成したいと考えています。POST 変数だけのパラメーターを持つ PHP スクリプトにアクセスしています。各スクリプトから返されるのは JSON 文字列です。通常、HTML フォームでは、次のようなリクエストを作成できます。
<form method="post" action="url.php">
<input name="this">
<input name="that">
<input value="Submit">
</form>
入力に応じて、送信をクリックした後、JSON data1 は次のようなものを返します。{ "code" : 1 }
スクリプトやスクリプトをホストするサーバーにアクセスできません。
Angular.js が JSON データ 1 を読み取り、そのデータ 1 を JSON データ 2 で定義されているものと照合して、それらをビュー ( <pre>data2</pre>
) に出力できるかどうか疑問に思っていました。
たとえば、{ "code" : 1 }
が取得された場合、JSON でコード #1 の値を出力する必要があります。
{ "code" : [
{ 1: "User already logged in." },
{ 2: "Wrong parameters, try again."},
{ 3: "etc., etc." }
]
};
これが私の試みです:
<form ng-controller="PhpCtrl" name="f1">
<input type="text" name="name">
<input type="text" name="password">
<pre ng-model="codeStatus">{{codeStatus}}</pre>
<input type="submit" ng-click="add()" value="Submit">
</form>
function PhpCtrl($scope, $http, $templateCache) {
$scope.method = 'POST';
$scope.url = 'url.php';
$scope.codeStatus = "";
$scope.add = function() {
$http({
method: $scope.method,
url: $scope.url,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
cache: $templateCache
}).
success(function(response) {
$scope.codeStatus = response.data;
}).
error(function(response) {
$scope.codeStatus = response || "Request failed";
});
return false;
};
}
HTTP/1.1 200 を処理していますが、これまでのところビューに投稿されているのは「要求が失敗しました」だけです。まだ道があることはわかっていますが、助けていただければ幸いです。適切な JSON データ 1 をビューにポストする方法がわかったら、次のステップは、適切なデータ 2 を照合して出力することです。前もって感謝します!