angular.js 自带jsonp,实现跨域,下面来实搜索框的下拉列表,使用百度和360分别尝试一下

百度:url截取之后红色部分需替换 :https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=数据&cb=JSON_CALLBACK

360:https://sug.so.360.cn/suggest?callback=JSON_CALLBACK&word=数据

注意:需要在服务器环境下运行

思路:

  1 .声明 angular

  2 .控制器函数中调用$http服务,用来读取web服务器上的数据

  3 .绑定数据$scope.data=[] 用来存放返回的数据

  4 .绑定函数$scope.show=function(){} ,在keyup的时候执行

  5 .调用$http.jsonp(url)

  6 .返回结果赋值给$scope.data ,$scope.data=data.s;

下面代码

 <!doctype html>
<html ng-app="app">
<head>
<meta charset="utf-8">
<style>
</style>
<script src="angular.js"></script>
<script>
var app=angular.module('app',[]); //声明
app.controller('test',function ($scope,$http){ // $http 是一个用于读取web服务器上数据的服务。
$scope.data=[]; // 绑定数据 $scope.show=function (){
// $http.jsonp(url) 是用于读取服务器数据的函数。
$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd='+$scope.t1+'&cb=JSON_CALLBACK').success(function (data){
//返回的结果赋值给$scope.data
$scope.data=data.s;
});
};
});
</script>
</head> <body ng-controller="test">
<div>
<!-- 输入内容绑定为t1 ,ng-keyup="show()" -->
<input type="text" ng-model="t1" ng-keyup="show()" />
</div>
<ul>
<!-- 数据展示 -->
<li ng-repeat="item in data">{{item}}</li>
</ul>
</body>
</html>

angular实现跨域的更多相关文章

  1. Angular http跨域

    原文:Angular http跨域 - 飞翔的小仓鼠 - 博客园 var app = angular.module('Mywind',['ui.router']); app.controller('M ...

  2. angular 的跨域处理

    angular 的跨域处理,由于不并不支持跨域处理,所以我们对他也是无可奈何,angular 的跨域处理,需要我们在服务器上进行处理,把请求的数据改成 * ,也就是,谁都可以请求数据: angular ...

  3. angular之跨域

    一.什么是跨域? 跨域是指一个域下的文档或者脚本去请求另一个域下的资源.(广义) 广义的跨域: 1.资源跳转:链接跳转.重定向.表单提交. 2.资源嵌入:<link>.<script ...

  4. angular.js跨域post解决方案

    跨域,前端开发中经常遇到的问题,AngularJS实现跨域方式类似于Ajax,使用CORS机制. 下面阐述一下AngularJS中使用$http实现跨域请求数据. AngularJS XMLHttpR ...

  5. angular的跨域(angular百度下拉提示模拟)和angular选项卡

    1.angular中$http的服务: $http.get(url,{params:{参数}}).success().error(); $http.post(url,{params:{参数}}).su ...

  6. angular cors跨域资源共享设置 和formdata设定

    非常easy,下来容易找到: <pre name="code" class="javascript">.config(['$routeProvide ...

  7. angular解决跨域问题

    通过angular自身的代理转发功能 配置package.json  启动项目通过npm start启动,会自动启动代理服务npm start

  8. angular http 跨域访问

    1.在跟目录中创建配置文件:proxy.config.json ,文件内容如下: { "/api": { "target": "http://192. ...

  9. angularJS1笔记-(18)-$http及用angular实现JSONP跨域访问过程

    官网上的解释为: The $http service is a core AngularJS service that facilitates communication with the remot ...

随机推荐

  1. iOS 旋转

    实现一张图片的旋转部分角度显示: [cpp] view plaincopy UIImageView image = [[UIImageView alloc]init]; image.frame = C ...

  2. CodeForces 625B War of the Corporations

    暴力匹配+一点判断 #include <stdio.h> #include <algorithm> #include <string.h> #include < ...

  3. 2、手把手教你Extjs5(二)项目中文件的加载过程

    上一节中用sencha工具自动创建了一个项目,并且可以在浏览器中查看.现在我们来看看js类加载过程.如下图所示: 1、首先:浏览器中输入 localhost:1841 ,调用 index.html; ...

  4. laravel数据库迁移的migrate小解

    当通过命令行:php artisan migrate:make create_authors_table --table=authors --create时,在 migration.php 中若Sch ...

  5. FZU 1063 三维扫描

    水题.DFS求连通块. #include<cstdio> #include<cstring> #include<cmath> #include <iomani ...

  6. Android源码编译jar包BUILD_JAVA_LIBRARY 与BUILD_STATIC_JAVA_LIBRARY的区别(二)

    上文简单介绍了BUILD_JAVA_LIBRARY 与BUILD_STATIC_JAVA_LIBRARY编译出来jar包的区别, 那么你如果拿到了一个内容是dex格式的jar包,而你又偏偏需要这个ja ...

  7. uoj#38. 【清华集训2014】奇数国【欧拉函数】

     number⋅x+product⋅y=1  有整数x,y解的条件是gcd(number, product) == 1. product用线段树维护一下,然后现学了个欧拉函数. 可以这样假如x = p ...

  8. iOS开发——闪光灯

    还是那句很欠揍的话,没啥难度,直接上代码. // //  ViewController.m //  Demo—闪光灯 // //  Created by yyt on 16/4/21. //  Cop ...

  9. iOS开发——代理与block传值

    一.代理传值的方法 1.Hehe1ViewController.h中 #import <UIKit/UIKit.h> @protocol Hehe1ViewControllerDelega ...

  10. DrawerLayout学习笔记

    基本步骤: 1>在XML中将DrawerLayout作为根视图 2>根视图中放两个View,第一个是主视图,即DrawerLayout隐藏的时候的视图,第二是就DrawerLayout的视 ...