jq-跨域请求
来源:互联网 发布:在淘宝上怎么修改评价 编辑:程序博客网 时间:2024/06/05 10:27
在使用的时候,常用的是跨域请求:
跨域问题解决方案:
1.使用script标签,及json数据需要赋值给变量e.g : <script type="text/javascript" src="http://169.254.4.210/dish.json"></script>
e.g : var json={"Id":2,"name":"豆角炒肉"};
2.使用ajax请求
jquery 的跨域请求
注意: 1) 设置datatype为jsonp ,jsonp 为callback,其次为callback设置返回函数
2)这里的callback函数名是已经存在的解析函数,例如go,这个存在,
则下面的success方法不会执行。
3) 最后是json数据封装的时候,必须用函数名称来封装,例如下面列子中
e.g : json数据封装格式:go({"Id":2,"name":"豆角炒肉"}); function go(json){ console.log(json.length); for(var j=0;j<json.length;j++){ console.log(json[j].context); } } $(function(){ console.log('jquery 执行了'); $.ajax({ type:"get", async:false, url:'http://169.254.4.210/dish.json', dataType:"jsonp", jsonp:'callback', callback:'go', success:function(data){ } });});
3.第三种方式是 动态的添加 script 标签。
e.g :封装创建script的函数 function getJson(url){ var script=document.createElement('script'); script.type='text/javascript'; script.src=url; document.head.appendChild(script); }
调用:1)直接调用,调用后 延迟解析,因为创建后需要加载(不推荐),
json数据封装为第一种格式; e.g :
getJson('http://169.254.4.210/dish.json'); setTimeout(function(){ for(var j=0;j<json.length;j++){ console.log(json[j].name); } },2000);
2)直接调用 ,在url里添加callback函数名,使用方便,需要声明
解析函数,数据封装格式为第2种方式
function go(json){ console.log(json.length); for(var j=0;j<json.length;j++){ console.log(json[j].context); } }
0 0
- jq-跨域请求
- jq ajax 跨域请求
- Jq 跨域问题
- jq的异步请求
- 原生/jq ajax请求
- jq ajax请求
- 解决html用js或者jq跨域请求报错的问题。我的ssm服务器!
- JQ跨域访问方法
- node中,已经在app中设置了跨域的问题,但是用jq的ajax请求时仍然报跨域错误
- jq中json请求的写法!
- jQ
- jQ
- jq
- jq
- JQ
- jq
- jQ
- jQ
- STL
- Setting Up Continuous Integration & Continuous Deployment With Jenkins
- 欢迎使用CSDN-markdown编辑器
- codeforces 277E - Binary Tree on Plane (MinCostMaxFlow, 简单)
- Checking for Undefined, Null, and Empty Variables in JavaScript
- jq-跨域请求
- 我真的不懂微信营销(一)
- failed to sync branch You might need to open a shell and debug the state of this repo.
- 单链表实现插入排序
- 生命是不断努力,持续累积的结果
- 系统启动及进程管理
- php递归获取顶级父类id
- C语言的数组初始化
- 找工作知识储备(3)---从头说12种排序算法:原理、图解、动画视频演示、代码以及笔试面试题目中的应用