当前位置:Gxlcms > html代码 > angular的post传参后台php无法接收_html/css_WEB-ITnose

angular的post传参后台php无法接收_html/css_WEB-ITnose

时间:2021-07-01 10:21:17 帮助过:10人阅读

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

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

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

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

解决方案:

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

angular.module('MyModule', [], function($httpProvider) {  // Use x-www-form-urlencoded Content-Type  $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';   /**   * The workhorse; converts an object to x-www-form-urlencoded serialization.   * @param {Object} obj   * @return {String}   */   var param = function(obj) {    var query = '', name, value, fullSubName, subName, subValue, innerObj, i;          for(name in obj) {      value = obj[name];              if(value instanceof Array) {        for(i=0; i

人气教程排行