Ajax请求后台数据

又犯错误了,真的是气人,现在把工作中的低级错误,拿出来批斗一下

注意:Ajax请求页面后台有这个么几个需要注意的地方

1.后台方法的一定是static,静态方法

2.后台方法中要加上WebMethod标示

    [WebMethod]
    public static string ABC() 
    {
        return "ABC";
    }

现在把常用的Ajax修改一下,变成通用的Ajax


function jsonAjax(url, data, callback, failure) {
            $.ajax({
                type: "post",
                url: url,
                data: data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: callback,
                error: failure
            });
        }

function callback(data)
        {
            alert(data.d);
        }

function failure() {
            alert("错误");
        }


调用Ajax的做法 无参调用

    $(function () {
            $("#Button1").bind("click", function () {
                jsonAjax("default.aspx/ABC", "", callback);
            }
            );
        })
调用Ajax的做法 有参调用

        $(function () {
            $("#Button1").bind("click", function () {
                jsonAjax("default.aspx/ABC1", "{'name':'"+"rex"+"'}", callback);
            }
            );
        })


现在把代码案例贴出来

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="jquery.min.js"></script>
    <script>
        $(function () {
            $("#Button1").bind("click", function () {
                jsonAjax("default.aspx/ABC", "", callback);
                jsonAjax("default.aspx/ABC1", "{'name':'"+"rex"+"'}", callback);
            }
            );
        })
function jsonAjax(url, data, callback, failure) {
            $.ajax({
                type: "post",
                url: url,
                data: data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: callback,
                error: failure
            });
        }
function callback(data)
        {
            alert(data.d);
        }
function failure() {
            alert("错误");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input id="Text1" type="text" value='张三'/>
        <input id="Button1" type="button"  value="提交" />
    </div>
    </form>
</body>
</html>

后台:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["name"] != null)
        {
            Response.ContentType = "text/json";
            Response.Write("{\"msg\":\"" + Request["name"] + "\"}");//将数据拼凑为Json
            Response.End();
        }
    }

    [WebMethod]
    public static string ABC() 
    {
        return "ABC";
    }


    [WebMethod]
    public static string ABC1(string name)
    {
        return "ABC " +name;
    }
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值