Home  >  Article  >  Web Front-end  >  angular的post传参后台php无法接收 - 范丁文

angular的post传参后台php无法接收 - 范丁文

WBOY
WBOYOriginal
2016-05-21 08:35:141093browse

很多时候我们需要用ajax提交post数据,angularjs与jq类似,也有封装好的post。

但是jQuery的post明显比angularjs的要简单一些,人性化一些。

两者看起来没什么区别,用angularjs的$http提交的数据,在php服务器端却无法通过$_REQUEST/$_POST获取到。

这是因为两者的post对header的处理有所不同……jQuery会把作为JSON对象的myData序列化,而Angular不会。

解决方案:

修改Angular的$httpProvider的默认处理(最完美的解决方案)

angular.module('MyModule', [], <span style="color: #0000ff;">function</span><span style="color: #000000;">($httpProvider) {
  </span><span style="color: #008000;">//</span><span style="color: #008000;"> Use x-www-form-urlencoded Content-Type</span>
  $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'<span style="color: #000000;">;
 
  </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
   * The workhorse; converts an object to x-www-form-urlencoded serialization.
   * @param {Object} obj
   * @return {String}
   </span><span style="color: #008000;">*/</span> 
  <span style="color: #0000ff;">var</span> param = <span style="color: #0000ff;">function</span><span style="color: #000000;">(obj) {
    </span><span style="color: #0000ff;">var</span> query = ''<span style="color: #000000;">, name, value, fullSubName, subName, subValue, innerObj, i;
      
    </span><span style="color: #0000ff;">for</span>(name <span style="color: #0000ff;">in</span><span style="color: #000000;"> obj) {
      value </span>=<span style="color: #000000;"> obj[name];
        
      </span><span style="color: #0000ff;">if</span>(value <span style="color: #0000ff;">instanceof</span><span style="color: #000000;"> Array) {
        </span><span style="color: #0000ff;">for</span>(i=0; i<value.length style="color: #000000;">i) {
          subValue =<span style="color: #000000;"> value[i];
          fullSubName </span>= name + '[' + i + ']'<span style="color: #000000;">;
          innerObj </span>=<span style="color: #000000;"> {};
          innerObj[fullSubName] </span>=<span style="color: #000000;"> subValue;
          query </span>+= param(innerObj) + '&'<span style="color: #000000;">;
        }
      }
      </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(value <span style="color: #0000ff;">instanceof</span><span style="color: #000000;"> Object) {
        </span><span style="color: #0000ff;">for</span>(subName <span style="color: #0000ff;">in</span><span style="color: #000000;"> value) {
          subValue </span>=<span style="color: #000000;"> value[subName];
          fullSubName </span>= name + '[' + subName + ']'<span style="color: #000000;">;
          innerObj </span>=<span style="color: #000000;"> {};
          innerObj[fullSubName] </span>=<span style="color: #000000;"> subValue;
          query </span>+= param(innerObj) + '&'<span style="color: #000000;">;
        }
      }
      </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(value !== undefined && value !== <span style="color: #0000ff;">null</span><span style="color: #000000;">)
        query </span>+= encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&'<span style="color: #000000;">;
    }
      
    </span><span style="color: #0000ff;">return</span> query.length ? query.substr(0, query.length - 1<span style="color: #000000;">) : query;
  };
 
  </span><span style="color: #008000;">//</span><span style="color: #008000;"> Override $http service's default transformRequest</span>
  $httpProvider.defaults.transformRequest = [<span style="color: #0000ff;">function</span><span style="color: #000000;">(data) {
    </span><span style="color: #0000ff;">return</span> angular.isObject(data) && String(data) !== '[object File]' ?<span style="color: #000000;"> param(data) : data;
  }];
});</span></value.length>

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn