Chinaunix首页 | 论坛 | 博客
  • 博客访问: 407962
  • 博文数量: 155
  • 博客积分: 2590
  • 博客等级: 少校
  • 技术积分: 2161
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-25 09:33
文章分类

全部博文(155)

文章存档

2015年(1)

2014年(2)

2013年(55)

2012年(97)

分类: 系统运维

2012-10-29 17:17:35

今天研究了AJAX使用JSONP进行跨域调用的方法,发现使用GET方式和POST方式都可以进行跨域调用,

代码如下

    客户端代码

  

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApp.WebForm1" %>  
  2.   
  3. >  
  4.   
  5. <html xmlns="" >  
  6. <head runat="server">  
  7. <script src="jquery-1.7.1.min.js" type="text/javascript">script>  
  8. <script type="text/javascript">  
  9.   
  10.     function aa() {  
  11.         $.ajax({  
  12.             url: "",  
  13.             data: "p1=1&p2=2&callback=?",  
  14.             type: "post",  
  15.             processData: false,  
  16.             timeout: 15000,  
  17.             dataType: "jsonp",  // not "json" we'll parse  
  18.             jsonp: "jsonpcallback",  
  19.             success: function(result) {  
  20.             alert(result.value1);  
  21.             }  
  22.         });  
  23.     }  
  24.    
  25.   
  26. script>  
  27.     <title>title>  
  28. head>  
  29. <body>  
  30.     <form id="form1" runat="server">  
  31.     <div>  
  32.       
  33.     div>  
  34.     form>  
  35.     <p>  
  36.         <input id="Button1" type="button" value="button" onclick="aa()" />p>  
  37. body>  
  38. html>  


服务器端代码

    

[csharp] view plaincopy
  1. public partial class WebForm2 : System.Web.UI.Page  
  2.    {  
  3.        protected void Page_Load(object sender, EventArgs e)  
  4.        {  
  5.             
  6.             
  7.         string callback = Request["callback"];   
  8.            string v1="1";  
  9.            string v2="2";  
  10.            string response = "{\"value1\":\"" + v1 + "\",\"value2\":\"" + v2 + "\"}";  
  11.            string call = callback + "(" + response + ")";  
  12.            Response.Write(call);  
  13.            Response.End();  
  14.        }  
  15.    }  


客户端页面和服务器端页面在两个项目中,以便进行跨域调用测试。

阅读(1267) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~