今天研究了AJAX使用JSONP进行跨域调用的方法,发现使用GET方式和POST方式都可以进行跨域调用,
代码如下
客户端代码
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApp.WebForm1" %>
-
- >
-
- <html xmlns="" >
- <head runat="server">
- <script src="jquery-1.7.1.min.js" type="text/javascript">script>
- <script type="text/javascript">
-
- function aa() {
- $.ajax({
- url: "",
- data: "p1=1&p2=2&callback=?",
- type: "post",
- processData: false,
- timeout: 15000,
- dataType: "jsonp", // not "json" we'll parse
- jsonp: "jsonpcallback",
- success: function(result) {
- alert(result.value1);
- }
- });
- }
-
-
- script>
- <title>title>
- head>
- <body>
- <form id="form1" runat="server">
- <div>
-
- div>
- form>
- <p>
- <input id="Button1" type="button" value="button" onclick="aa()" />p>
- body>
- html>
服务器端代码
- public partial class WebForm2 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
-
- string callback = Request["callback"];
- string v1="1";
- string v2="2";
- string response = "{\"value1\":\"" + v1 + "\",\"value2\":\"" + v2 + "\"}";
- string call = callback + "(" + response + ")";
- Response.Write(call);
- Response.End();
- }
- }
客户端页面和服务器端页面在两个项目中,以便进行跨域调用测试。
阅读(1311) | 评论(0) | 转发(0) |