anglarJSでpostメソッドを使用した場合、バックグラウンドの値が得られない問題を素早く解決します。


anglarJSのhttpサービスで一つの問題にぶつかりました。http.postメソッドを使ってバックグラウンドにデータを転送する場合、バックグラウンドのphpページはdataパラメータからの値を取得できません。
この姿勢に関係なく:

$http.post( "1.php", { id: 1 }).success(function (data) {
  console.log(data);
  });
それともこのような姿勢ですか?

$http({
 method: 'POST',
 url: '1.php',
 data: { id: 1 }
 }).success(function (data) {
 console.log(data);
 });
バックグラウンドphpの中の$POSTまたは$uREQUSTはすべてdataの値を取得できませんでした。

<?php
 echo json_encode($_POST);
?>
出力は空の行列です。php自体が本当に値を得られないかをテストするために、フォームテストを書きました。

<form action="1.php" method="post">
 <input type="text" name="tid">
 <input type="submit" value="submit">
</form>
出力結果は「{tid}2」ということで、フォームの値は取得できますが、ajaxで送信されたデータは取得できません。
フォームデータとajax送信のpostデータの違いは何ですか?そこで私はそっと首を…
1.フォームの要求ヘッダ:

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8,ja;q=0.6
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 5
Content-Type: application/x-www-form-urlencoded
Cookie: a0537_times=1
Host: 127.0.0.1
Origin: http://127.0.0.1
Pragma: no-cache
Referer: http://127.0.0.1/angularTest/1.html
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
2.ajaxが送信するデータの要求ヘッダ:

Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8,ja;q=0.6
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 10
Content-Type: application/json;charset=UTF-8
Cookie: a0537_times=1
Host: 127.0.0.1
Origin: http://127.0.0.1
Pragma: no-cache
Referer: http://127.0.0.1/angularTest/1.html
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
問題が急に出ました。フォーム送信のテキストタイプはフォームタイプで、anglarのajaxはデフォルトで送るのはjsonデータです。
どうしてConteet-typeを変えましたか?そこで私はangglarの公式サイトを開きました。変更要求に従ってください。

$http({
 method: 'POST',
 url: '1.php',
 data: { id : 1 }
 headers: {
  'Content-Type': 'application/x-www-form-urlencoded'
 }
 }).success(function (data) {
 console.log(data);
 });
すると出力結果は「\」test\:「1」:「\」です。まだ問題があります。対象は自動的に序列化されていませんでした(jQueryは慣れていますので、忘れてしまいます。この問題があります。)
では、解決策は以下の通りです。
1.対象としない形式で、直接文字列を書きます。

$http({
 method: 'POST',
 url: '1.php',
 data: 'test=1',
 headers: {
  'Content-Type': 'application/x-www-form-urlencoded'
 }
 }).success(function (data) {
 console.log(data);
 });
2.アングラーの中のトランスフォムRequestを書き直して、自分で変換方法を書きます。

 $http({
 method: 'POST',
 url: '1.php',
 data: $scope.data,
 headers: {
  'Content-Type': 'application/x-www-form-urlencoded'
 },
 transformRequest: function ( data ) {
  var str = '';
  for( var i in data ) {
  str += i + '=' + data[i] + '&';
  }
  return str.substring(0,str.length-1);
 }
 }).success(function (data) {
 console.log(data);
 });
3.アングラーの中のトランスフォームRequestを書き直して、簡単に乱暴にjqueryを持ってきます。

 $http({
 method: 'POST',
 url: '1.php',
 data: $scope.data,
 headers: {
  'Content-Type': 'application/x-www-form-urlencoded'
 },
 transformRequest: function ( data ) {
  return $.param(data);
 }
 }).success(function (data) {
 console.log(data);
 });
4.デフォルトのtransformationsを修正します。
Default Trans formations

The $httpProvider provider and $http service expose defaults.transformRequest and defaults.transformResponse properties. If a request does not provide its own transformations then these will be applied.

You can augment or replace the default transformations by modifying these properties by adding to or replacing the array.

Angular provides the following default transformations:

Request transformations ($httpProvider.defaults.transformRequest and $http.defaults.transformRequest):

If the data property of the request configuration object contains an object, serialize it into JSON format.
Response transformations ($httpProvider.defaults.transformResponse and $http.defaults.transformResponse):

If XSRF prefix is detected, strip it (see Security Considerations section below).
If JSON response is detected, deserialize it using a JSON parser.
そのままコピーします

app.config(['$httpProvider', function ( $httpProvider ) {
  $httpProvider.defaults.transformRequest = function ( data ) {
  var str = '';
  for( var i in data ) {
   str += i + '=' + data[i] + '&';
  }
  return str.substring(0,str.length-1);
  }
 }]);

<code class="language-javascript">$http({ 
 method: 'POST', 
 url: '1.php', 
 data: $scope.data, 
 headers: { 
  'Content-Type': 'application/x-www-form-urlencoded' 
 } 
 }).success(function (data) { 
 console.log(data); 
 });</code> 
以上のこの快速解決のanglarJSでpostメソッドを使う時のバックグランドの値が取れない問題は小編集が皆さんに共有した内容の全部です。参考にしてもらいたいです。どうぞよろしくお願いします。