博客
关于我
angular-请求数据
阅读量:228 次
发布时间:2019-02-28

本文共 4764 字,大约阅读时间需要 15 分钟。

问题一:

angularjs $http 中的data传递参数后端没法接收 但是用params传递参数参数在url后后端能接收 ?

GET请求的时候用params,
POST/PUT/PATCH/DELETE请求时用data.

根据官方文档:params – {Object.<string|Object>} – Map of strings or objects which will be serialized with theparamSerializer and appended as GET parameters.data – {  string|Object} – Data to be sent as the request message data.

示例:post请求用data

     var promise = $http({                method: 'POST',                url: reqUrl,                data: reqParams            });

问题二:

Angular 1.6提示

angular.js:14525 TypeError: $http(...).success is not a function(…)

原因:
在1.6版本的AngularJs中取消了http的success和error方法。注意在1.6版本及以上使用时去掉success和error方法。

angular的post请求数据方法汇总:

方法一:

在angularjs版本1.6以下可使用:

//注意:在angular的1.6版本及以上不支持            $http({                method: 'POST',                url: reqUrl,                data: reqParams            }).success(function (data, status, headers, config) {                   console.log("===========req4 response data============" + JSON.stringify(data));                console.log("===========req4 response status============" + JSON.stringify(status));                console.log("===========req4 response config============" + JSON.stringify(config));                console.log("===========req4 response headers============" + JSON.stringify(headers));                //statusText为undefined,已经是success            }).error(function (data, status, headers, config) {                   console.log("===========req4 response error============" + JSON.stringify(data));            });

或

方法二:

//注意:在angular的1.6版本及以上不支持            var deferred = $q.defer();            $http({                method: 'POST',                url: reqUrl,                data: reqParams            }).success(function (data, status, headers, config) {                   deferred.resolve(data);            }).error(function (data, status, headers, config) {                   deferred.reject(data);            });            var promise = deferred.promise;            promise.then(function (response) {                   console.log("req4 response============" + JSON.stringify(response));            });

$q是Angular的一种内置服务,它可以使你异步地执行函数,并且当函数执行完成时它允许你使用函数的返回值(或异常)。

defer的字面意思是延迟,$q.defer() 可以创建一个deferred实例(延迟对象实例)。

deferred 实例旨在暴露派生的Promise 实例,以及被用来作为成功完成或未成功完成的信号API,以及当前任务的状态。

$q, defer, promise三者之间的关系如下所示。

var deferred = $q.defer(); //通过$q服务注册一个延迟对象 deferredvar promise = deferred.promise; //通过deferred延迟对象,可以得到一个承诺promise,//而promise会返回当前任务的完成结果

方法三:

在angular所有版本均支持:

//所有angular版本均支持,推荐使用            var promise = $http({                method: 'POST',                url: reqUrl,                data: reqParams            });            promise.then(function (response) {                   console.log("req1 response=====" + JSON.stringify(response));                console.log("req1 response data======" + JSON.stringify(response.data));                console.log("req1 response status======" + JSON.stringify(response.status));                console.log("req1 response config=====" + JSON.stringify(response.config));                console.log("req1 response headers=====" + JSON.stringify(response.headers));                console.log("req1 response statusText=====" + JSON.stringify(response.statusText));            });

方法四:

ajax请求:需要添加jquery

    <script src="../jquery.js"></script>
//ajax请求数据            $.ajax({                type: "POST",//请求方式                url: reqUrl,                data: JSON.stringify(reqParams),//请求参数,必须字符串                dataType: "json",//                headers: {  'Content-Type': 'application/json'},//要添加请求头                error: function () {                       alert("hello");                },                success: function (data, status) {                       console.log("req3 response=====" + JSON.stringify(data));                    console.log("req3 status===" + JSON.stringify(status));//success                }            });

方法五:

XMLHttpRequest请求:

 //XMLHttpRequest请求:            var xmlhttp;            if (window.XMLHttpRequest) {  // code for IE7+, Firefox, Chrome, Opera, Safari                xmlhttp = new XMLHttpRequest();            } else {  // code for IE6, IE5                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");            }            xmlhttp.onreadystatechange = function () {                   if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {                    console.log("req1 response====" + (xmlhttp.responseText));//string                    console.log("req1 responseCode======" + JSON.parse(xmlhttp.responseText).responseCode);                }            };            xmlhttp.open("POST", reqUrl, true);            xmlhttp.setRequestHeader("Content-type", 'application/json');            xmlhttp.send(JSON.stringify(reqParams));

转载地址:http://vcss.baihongyu.com/

你可能感兴趣的文章
Python-通过函数传递变量
查看>>
Python内置函数详解——总结篇
查看>>
Python.对象集合中的标识.和散列
查看>>
Python.硒.拖放错误“AttributeError:move_to 需要 WebElement“
查看>>
Python内置函数详解
查看>>
Python/Kivy:从一个类调用函数到另一个类并在 Python 中显示小部件
查看>>
Python/Kivy:我可以在双击标签时调用函数吗
查看>>
python内置函数5-frozenset()
查看>>
Python/Plotly:如何使用要显示的信息自定义悬停模板?
查看>>
Python/Selenium - 无法单击 www.instagram.com 上的“接受 cookie“按钮
查看>>
Python内置函数(35)——next
查看>>
python写入mysql
查看>>
python写入csv老是乱码或者各种不好
查看>>
Python内置函数
查看>>
Python2 安装MySQLdb库
查看>>
Python2 已终结,入手Python 3,你需要这30个技巧
查看>>
python写下拉列表在excel,无法使用python获取Excel下拉列表(组合框)值
查看>>
python2.7安装
查看>>
Python2.7安装ncmbot时提示:Microsoft Visual C++9.0 is required
查看>>
PYTHON2.Git.1
查看>>