AngularJS $http服务实现跨域名访问(jsonp)

JQuery实现跨域名访问(jsonp):https://blog.csdn.net/houyanhua1/article/details/79703731


demo.html:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>AngularJS</title>
	<script src="angular.min.js"></script>  <!-- 引入AngularJS框架 -->
</head>
<body ng-app="App">
	<div ng-controller="DemoController">
	</div>
	<script>
		
		var App = angular.module('App',[]);

		// $http服务实现跨域名请求(jsonp)
		App.controller("DemoController",['$scope','$http',function($scope,$http) {
			// jsonp和ajax没有任何关系。 JQuery中的Ajax(jsonp)也是利用<script>标签的src属性。

			$http({
				url: 'www.其他域名.com/jsonp.php?callback=JSON_CALLBACK', // JSON_CALLBACK是一个占位符
				params: {
					//callback: JSON_CALLBACK,  // 可以将url中的callback写到params中。
					name: "张三",
					age: 12,
				}
				method: 'jsonp',  // 利用<script>标签的src属性,此时不是Ajax请求。
			}).success(function(data) {  // 该函数就是传给后端服务器的回调函数。
				// ....
			});

		}]);
		
	</script>
</body>
</html>




猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/80150976