AngularJS 报错: $http(...).success is not a function

本文介绍AngularJS 1.6版本中HTTP请求方法的变化,详细解释了从使用success和error回调到采用Promise规则的方法转变。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用Angular 1.6版本的 http http(…).success is not a function;
异常代码如下:

$http({
    url: './stars.php',
    method: 'get'
}).success(function (info) {
            $log.info(info);
            $scope.stars = info;
}); 

查询了原因,是新版本的AngularJs中取消了success和error,用promise规则。

$http({
    url: './stars.php',
    method: 'get'
}).then(function ( info ){
    $scope.stars = info;
})
这个错误可能是因为你的代码中使用了某个函数或方法,但该函数或方法不存在或者不是一个函数。 在你的代码中,可能是因为使用了 createApp 方法,但是该方法在 Vue.js 2.x 中是不存在的。如果你的代码是在 Vue.js 3.x 中运行,那么你需要使用 createApp 方法来创建 Vue 实例。 如果你的代码是在 Vue.js 2.x 中运行,那么你需要使用 new Vue(options) 的方式来创建 Vue 实例。如果你需要使用动态组件,可以使用 keep-alive 组件来缓存组件。 下面是一个示例代码: ``` <template> <keep-alive> <component :is="dynamicComponent" /> </keep-alive> </template> <script> import axios from 'axios' export default { data() { return { dynamicComponent: 'loading', componentCode: null } }, async mounted() { const componentCode = await this.fetchComponentCode() this.componentCode = componentCode const component = this.createDynamicComponent(componentCode) this.dynamicComponent = component }, methods: { async fetchComponentCode() { let param = { table: 't_code', where: " and ky='vue-test' " } try { const response = await axios.post('/base/crud/list', param) if (response.data.success) { return response.data.data[0].code } } catch (error) { console.error(error) } }, createDynamicComponent(code) { const options = { template: code, data() { return {} }, mounted() {} } return Vue.extend(options) } } } </script> ``` 在这个示例代码中,我们使用了 Vue.extend 方法来创建动态组件。同时,我们使用了 keep-alive 组件来缓存组件,以提高性能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值