首页 > 语言 > JavaScript > 正文

jquery ajax jsonp跨域调用实例代码

2024-05-06 14:32:08
字体:
来源:转载
供稿:网友

客户端代码

代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApp.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    function aa() {
        $.ajax({
            url: "http://localhost:12079/WebForm2.aspx",
            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();

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选