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), <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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容