zoukankan      html  css  js  c++  java
  • AJAX校验商品价格(类似校验用户名)

    服务器端程序

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <%@ WebHandler Language="C#" Class= "GetPrice" %>
     
    using System;
    using System.Web;
     
    public class GetPrice : IHttpHandler {
        
        public void ProcessRequest (HttpContext context) {
            context .Response.ContentType = "text/plain";
            string name = context.Request ["name"];
            
            DataSetProductsTableAdapters .T_ProductsTableAdapter priceadapter=new DataSetProductsTableAdapters .T_ProductsTableAdapter();
            DataSetProducts .T_ProductsDataTable prices = priceadapter.GetDataByName(name);
            if ( prices.Count <=0)
            {
                context .Response.Write("none|0" );
            }
            else
            {
                DataSetProducts .T_ProductsRow price = prices[0 ];
                context .Response.Write("ok|" +Convert.ToString(price .Price));
            }
        }

    客户端页面代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    <script type="text/javascript">
            $( function () {
                $( "#Text1").blur(function () {
                    var sname = $("#Text1" ).val();
                    $.post( "GetPrice.ashx", { name: sname }, function (data, status) {
                        if (status == "success" ) {
                            var arrs = data.split("|" );
                            if (arrs[0] == "ok" ) {
                                $( "#Text2").val(arrs[1]);
                            }
                            else if (arrs[0] == "none") {
                                alert( "没有该商品" );
                            }
                            else {
                                alert( "AJAX错误");
                            }
                        }
                        else {
                            alert( "wrong");
                        }
     
                    });
                });
            })
        </script >
     
    <body>
     
        <p >
            <input id="Text1" type="text" />
            <input id="Text2" type="text" /></p>
     
    </body>
    </html>

    测试效果


  • 相关阅读:
    内存映射的原理
    Intel 面试(就不该报外企,英语是硬伤)
    基于多进程和基于多线程服务器的优缺点及nginx服务器的启动过程
    pdflush机制
    百度面试
    同步IO和异步IO的区别
    阿里面试
    linux内核学习之四:进程切换简述
    static成员函数不能调用non-static成员函数
    C/C++函数调用方式
  • 原文地址:https://www.cnblogs.com/zhxshseu/p/5285353.html
Copyright © 2011-2022 走看看