首页 技术 正文
技术 2022年11月9日
0 收藏 674 点赞 1,478 浏览 2573 个字

要实现JSONP跨域访问,首先就要了解什么是跨域?然后JSONP与JSON的关系?

1、什么是跨域?

跨域简单的说就是一个域名下的程序和另一个域名下的程序做数据交互。比如说:现有一个http://www.zq.com和http://www.qt.com两个独立的网站,要实现从后一个网站向前一个网站中取数据。把做为数据来源的当作服务端,把去获取数据的当作客户端,实现面向服务的编程。

在动态网页当中,最重要的一项就是取数据。如果是在同一个项目中,将网站挂在同一个域名下,这种情况下无论是前台Ajax还是后台直接取数据都非常方便。但是如果像上面的两个网站一样,在不同的域名下,那么取数据就变得非常麻烦(现在有Web Servers和WCF实现面向服务的编程)

2、什么情况下用JSONP做跨域访问?

当要用Ajax从其它的项目中取数据时候。如果是在后台中从其它网站取数据,可用Web Servers或WCF等技术来实现。

3、JSONP和JSON的关系?

JSON是一种基于文本的数据交互方式,而JSONP是基于JSON的使用方式。从使用上简单的讲就是:不跨域 用JSON实现A jax和后台数据交互; 跨域 用JSONP实现Ajax和其它域中程序做数据交互。

4、$.ajax实现JSONP

  1. <%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”PRequest.aspx.cs” Inherits=”jsonP.PRequest” %>
  2. <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
  3. <html xmlns=”http://www.w3.org/1999/xhtml”>
  4. <head>
  5. <script src=”Scripts/jquery-1.7.1.min.js” type=”text/javascript”></script>
  6. <script src=”Scripts/json2.js” type=”text/javascript”></script>
  7. <script type=”text/javascript”>
  8. function aa() {
  9. var person = {};//自定义数组
  10. person.name = “aName”;
  11. person.age = 18;
  12. //console.log(JSON.stringify(person));
  13. $.ajax({
  14. url: “http://localhost:58878/jp.ashx”,//另一个域中的程序
  15. type: “get”,
  16. dataType: “jsonp”,//预期返回类型为JSONP
  17. data: { ‘person’: JSON.stringify(person) },
  18. jsonp: “callback”, //在一个jsonp请求中重写回调函数的名字,可省
  19. success: function (rt) {
  20. var result = JSON.parse(rt);
  21. //console.log(result);
  22. $(“#lb”).html(result.Name + “<br />” + result.Age);
  23. }, error: function (e) {
  24. //console.log(e);
  25. }
  26. });
  27. }
  28. </script>
  29. </head>
  30. <body>
  31. <input id=”Button1″ type=”button” value=”取值” onclick=”aa()” />
  32. <div id = “lb” style=”width: 90px;height: 90px;background-color: red”></div>
  33. </body>
  34. </html>

通过分析不难发现其与JSON写法上的不同:

1、url上的细微差异,在JSONK中常是直接某个路径下的处理程序,而JSONP是写别的网站(域)下的处理代码程序路径。但要注意:JSON也能这样写

2、dataTypy现在处理的是JSONP而不是JSON

其它传参数据 ,接参数等和JSON并无二异

数据源部分

  1. using System.Web;
  2. namespace PResponse
  3. {
  4. /// <summary>
  5. /// Summary description for jp
  6. /// </summary>
  7. public class jp : IHttpHandler
  8. {
  9. public class person
  10. {
  11. public string Name { get; set; }
  12. public int Age { get; set; }
  13. }
  14. public void ProcessRequest(HttpContext context)
  15. {
  16. context.Response.ContentType = “text/plain”;
  17. string callback = context.Request[“callback”];//callback不能少
  18. string ca = context.Request[“person”];
  19. person b = Newtonsoft.Json.JsonConvert.DeserializeObject<person>(ca);
  20. string response = “{\”Name\”:\”” + b.Name + “\”,\”Age\”:\”” + b.Age + “\”}”;
  21. string re = Newtonsoft.Json.JsonConvert.SerializeObject(response);
  22. string call = callback + “(” + re + “)”;//response和re相同,前面的callback不能少
  23. context.Response.Write(call);
  24. }
  25. public bool IsReusable
  26. {
  27. get
  28. {
  29. return false;
  30. }
  31. }
  32. }
  33. }

这样就实现了跨域访问

5、说明

1)之所以在前台取数据要分JSON和JSONP,是因为JavaScript 的同源策略

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295