ajax和angular,从jquery $ .ajax到angular $ http

本文介绍了AngularJS中使用$http服务进行HTTP请求的方法,包括.post()的简洁方式以及回调函数的使用。AngularJS会自动处理JSON转换和设置头部。注意在AngularJS 1.5中,.success()和.error()已被弃用,推荐使用.then()处理响应。查阅官方文档以获取更多详细信息。
摘要由CSDN通过智能技术生成

8ccaa06752fcc4cefb3799efe3448600.png

茅侃侃

AngularJS调用$ http的方式如下所示:$http({    url: "http://example.appspot.com/rest/app",    method: "POST",    data: {"foo":"bar"}}).then(function successCallback(response) {        // this callback will be called asynchronously        // when the response is available        $scope.data = response.data;    }, function errorCallback(response) {        // called asynchronously if an error occurs        // or server returns response with an error status.        $scope.error = response.statusText;});或者可以使用快捷方式将其编写得更加简单:$http.post("http://example.appspot.com/rest/app", {"foo":"bar"}).then(successCallback, errorCallback);有很多事情要注意:AngularJS版本更简洁(尤其是使用.post()方法)AngularJS将负责将JS对象转换为JSON字符串并设置标头(可自定义)回调函数分别命名success和命名error(也请注意每个回调的参数)-angular v1.5中已弃用使用then功能代替。使用的更多信息then可以在这里找到上面只是一个简单的示例和一些指针,请务必查看AngularJS文档以获取更多信息:http : //docs.angularjs.org/api/ng.$http

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值