栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从jquery $ .ajax到angular $ http

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从jquery $ .ajax到angular $ http

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436848.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号