欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

angular POST请求的两种写法 有大用

angular POST请求的两种写法

96 
louhangfei 
2017.04.25 20:02* 字数 207 阅读 3832评论 0

正确写法1

$http.post(url, data).then(function (res) {
     console.log(res)
})

正确写法2

$http({
    method:"POST",
    url:config.createIcode,
    data:data
}).then(function (res) {
    console.log(res)
})

但是下面的写法是错误的

$http.post({
    url:config.createIcode,
    data:data
}).then(function (res) {
     console.log(res)
})

报错信息如下:

angular.min.js:123 Error: [$http:badreq] http://errors.angularjs.org/1.6.4/$http/badreq?p0=%7B%22url%22%3A%22http%3A…3A%22Datayes%22%2C%22title%22%3A%22X%22%2C%22source%22%3A%22inner%22%7D%7D
    at http://localhost:4000/js/externallibs/angular.min.js:6:425
    at n (http://localhost:4000/js/externallibs/angular.min.js:99:305)
    at Function.n.(anonymous function) [as post] (http://localhost:4000/js/externallibs/angular.min.js:105:172)
    at b.app.controller.$scope.submit (http://localhost:4000/js/app.js:46:19)
    at fn (eval at compile (http://localhost:4000/js/externallibs/angular.min.js:1:1), <anonymous>:4:138)
    at e (http://localhost:4000/js/externallibs/angular.min.js:284:187)
    at b.$eval (http://localhost:4000/js/externallibs/angular.min.js:148:347)
    at b.$apply (http://localhost:4000/js/externallibs/angular.min.js:149:52)
    at HTMLInputElement.<anonymous> (http://localhost:4000/js/externallibs/angular.min.js:284:239)
    at hg (http://localhost:4000/js/externallibs/angular.min.js:39:299)

在angular官网上找到如下错误提示

Error: $http:badreqBad Request Configuration
Http request configuration url must be a string or a $sce trusted object. Received: {"url":"http://120.132.23.53/icode","data":{"address":"hangfei.lo...}}
Description
This error occurs when the request configuration parameter passed to the $http
service is not a valid object. $http
expects a single parameter, the request configuration object, but received a parameter that was not an object or did not contain valid properties.
The error message should provide additional context such as the actual value of the parameter that was received. If you passed a string parameter, perhaps you meant to call one of the shorthand methods on $http
such as $http.get(…)
, etc.
To resolve this error, make sure you pass a valid request configuration object to $http
.
For more information, see the $http
service API documentation.


重点提示信息:

$http expects a single parameter, the request configuration object, but received a parameter that was not an object or did not contain valid properties.

小礼物走一走,来简书关注我

赞赏支持

来自 https://www.jianshu.com/p/f9ffc68e64ec


angularJS发起$http.post请求后台收不到数据解决方案

AngularJS发起$http.post请求

代码如下:

1
2
3
4
5
6
7
$http({
  method:'post',
  url:'post.php',
  data:{name:"aaa",id:1,age:20}
}).success(function(req){
  console.log(req);
})

这时候你会发现收不到返回的数据,结果为null,这是因为要转换成form data。 

解决方案:

配置$httpProvider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var myApp = angular.module('app',[]);
 myApp.config(function($httpProvider){
 
  $httpProvider.defaults.transformRequest = function(obj){
   var str = [];
   for(var p in obj){
    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
   }
   return str.join("&");
  }
 
  $httpProvider.defaults.headers.post = {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
 
});

或者在post中配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$http({
  method:'post',
  url:'post.php',
  data:{name:"aaa",id:1,age:20},
  headers:{'Content-Type': 'application/x-www-form-urlencoded'},
  transformRequest: function(obj) {
   var str = [];
   for(var p in obj){
    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
   }
   return str.join("&");
  }
}).success(function(req){
    console.log(req);
})


来自  https://www.cnblogs.com/gongshunkai/p/6752647.html




其实,angularjs 和 jquery js最大的区别在哪儿那,angularjs是你事先在心中构建好真个页面,然后用变量或者占位符来表示数据,数据来了,直接填充就可以了;而jquery则是动态的修改dom元素,如添加修改dom标签等。设计思想不一样。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

来自  https://www.csdn.net/gather_2a/Ntjagg1sMDQxLWJsb2cO0O0O.html



AngularJS发起$http.post请求

代码如下:

复制代码
$http({  
    method:'post',  
    url:'post.php',  
    data:{name:"aaa",id:1,age:20}  
}).success(function(req){  
    console.log(req);  
})  
复制代码

这时候你会发现收不到返回的数据,结果为null,这是因为要转换成form data。 
解决方案:

配置$httpProvider:

复制代码
var myApp = angular.module('app',[]);  
 myApp.config(function($httpProvider){  

   $httpProvider.defaults.transformRequest = function(obj){  
     var str = [];  
     for(var p in obj){  
       str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));  
     }  
     return str.join("&");  
   }  

   $httpProvider.defaults.headers.post = {  
        'Content-Type': 'application/x-www-form-urlencoded'  
   }  

});  
复制代码

或者在post中配置:

复制代码
$http({  
   method:'post',  
   url:'post.php',  
   data:{name:"aaa",id:1,age:20},  
   headers:{'Content-Type': 'application/x-www-form-urlencoded'},  
   transformRequest: function(obj) {  
     var str = [];  
     for(var p in obj){  
       str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));  
     }  
     return str.join("&");  
   }  
}).success(function(req){  
       console.log(req);  
})  
复制代码

 

分类: angular

来自 https://www.cnblogs.com/gongshunkai/p/6752647.html

https://blog.csdn.net/weixin_33858336/article/details/94747464

https://www.cnblogs.com/smallzhu/p/8182665.html


普通分类: