加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 教程 > 正文

jquery ajax jsonp跨域调用示例代码

发布时间:2023-08-21 14:30:01 所属栏目:教程 来源:
导读:客户端代码

代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApp.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN
客户端代码
 
代码如下:
 
<%@ 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();
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章