angular POST请求的两种写法

正确写法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), :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. (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.

你可能感兴趣的:(angular POST请求的两种写法)