asp.net 练习jsonp

<%@ WebHandler Language="C#" Class="ValidationOrderCount" %>

using System;
using System.Web;

using BLL.BLL0001;
using System.Data;
using System.Text;

/*
 *创建人:谈勇
 *时间:2012-10-10
 *重复订单校验
 */
public class ValidationOrderCount : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        int userId = context.Request["suserId"] == null ? 0 : int.Parse(context.Request["suserId"].ToString());
        int productId = context.Request["sproductId"] == null ? 0 : int.Parse(context.Request["sproductId"].ToString());
        string key = context.Request["orderJsonData"];
        
        StringBuilder strJsonData = new StringBuilder();//拼接json所有格式
        StringBuilder strJsonMsgData = new StringBuilder();//拼接json内容
        
        A001 a001 = A001.Instance();
        DataTable dt = a001.GetOrderNumberByPhone(userId, productId);
        if (dt.Rows.Count > 0)
        {
            string orderCode = dt.Rows[0]["OrderCode"].ToString();
            string orderId = dt.Rows[0]["id"].ToString();

            strJsonData.AppendFormat("{0}([", key);//json begin

            strJsonMsgData.Append("{\"jorderCode\"" + ":" + "\"" + orderCode + "\"" + ",\"jorderId\"" + ":" + "\"" + orderId
                            + "\"}" + ",");

            strJsonData.Append(strJsonMsgData.ToString().TrimEnd(','));

            strJsonData.Append("])");//json end

            context.Response.Write(strJsonData.ToString());
        }
        else
        {

            strJsonData.AppendFormat("{0}([", key);//json begin
            strJsonData.Append("])");//json end
            context.Response.Write(strJsonData.ToString());
        }
        
        
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}




<script type="text/javascript">
//重复订单脚本 谈勇 2012-10-11

    function ValidationOrder(userId, productId) {
        $.ajax({
            url: '../Ashx/ValidationOrderCount.ashx?suserId=' + userId + '&sproductId=' + productId,
            dataType: "jsonp",
            type: "GET",
            async: true,
            jsonp: "orderJsonData", //callback的function名称
            success: function(data) {
                if (data.length == 0) {
                    openNewOrderPage(userId, productId);
                }
                else{
                    
                    $.each(data, function(index, entry) {
                        var orderCode = entry['jorderCode']; //订单编号
                        var orderId = entry['jorderId']; // 订单id

                        var phtml = '';
                        phtml += '您已预订过相同线路出发的订单,订单号为' + orderCode + '<font id="fontOrderCode" style="font-size: 12px;"></font>(<a onclick="openEditOrderPage(' + orderId + ')"';
                        phtml += ' style="color: blue; text-decoration: underline;cursor:pointer;">查看</a>),是否确认重复下单?';
                     

                        $("#pMsg1").html(phtml);

                        //是按钮添加onclick事件
                        $("#btnGoOn").unbind().bind("click",function() {
                            openNewOrderPage(userId, productId);
                        })

                    });

                    editWidget2('#dv_Repeat', 200, 400);
                }
            },
            error: function() {
                alert("err");
            }
        });
    };

    function openEditOrderPage(orderID) {//跳到订单编辑页
        window.open('../ordertrip/Order003.aspx?id=' + orderID + '&rand=' + Math.random() + '');
    }

    function openNewOrderPage(userId, productId) {//跳到订单新增页
        $("#dv_Repeat").hide();  //关闭层
        $("#JS-layer").remove(); //移除笼罩

        window.open('../ordertrip/Order002.aspx?offline=0&uid=' + userId + '&pid=' + productId + '');
        
    }

</script>


如果 在each 循环注册 onclick 事件 要 解绑 重新绑定,如果不解绑,重绑 你会发现,在table 表格中的按钮 点击事件 很多都是无用的.

$("#btnGoOn").unbind().bind("click",function() { })


           <div class="login_pay_dh" id="dv_Repeat" style="display: none;">
                <div class="login_paybg_dh">
                    <div class="login_tit_dh">
                        <h1>
                            温馨提示</h1>
                        <img alt="关闭" src="http://www.csdn.com/content/reg/image/pwd/login_closebtn_dh.jpg"
                            onclick="closeEditorDiv(this,'#dv_Repeat');" style="cursor: pointer;" />
                    </div>
                    <div class="login_input_dh margin_right_dh">
                        <div class="P1_dh">
                            <p id="pMsg1" style="font-size: 12px; line-height: 22px;">
                            </p>
                        </div>
                        <div class="P2_dh">

                            <input name="btnGoOn" id="btnGoOn" type="button" value="是" style="cursor: pointer;" />
                            <input name="btnCancel" id="btnCancel" type="button" value="否" onclick="closeEditorDiv(this,'#dv_Repeat');" style="cursor: pointer;" />
                        </div>
                    </div>
                </div>
            </div>

看下我的请求 的地址

http://www.csdn.com:8008/Ashx/ValidationOrderCount.ashx?suserId=316892&sproductId=54448&orderJsonData=jsonp1350363953620&_=1350363955298


orderJsonData=jsonp1350363953620&_=1350363955298 拿到这节callback

asp.net 练习jsonp_第1张图片

jsonp格式


效果

asp.net 练习jsonp_第2张图片

处理订单提示


jquery api
http://www.css88.com/jqapi-1.6/#p=jQuery.ajax

你可能感兴趣的:(jsonp,function,Class,asp.net,login)