您的位置:首页 > Web前端 > JQuery

JQuery中ajax,get方法在ashx,aspx,asmx中的使用

2017-10-11 15:26 465 查看
这篇博客文章给了很大的提示,原地址是http://blog.csdn.net/lovegonghui/article/details/49072363

一、请求ashx的例子:

[html] view
plain copy

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

<!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">

<title>请求ashx的例子</title>

<script type="text/javascript" src="../Scripts/jquery-1.4.1.min.js"></script>

<script type="text/javascript">

$(function () {

//POST请求,数据类型为text

/* $.ajax({

type: "POST",

url: "Response.ashx",

contentType: "application/text;charset=utf-8;",

dataType: "text",

success: function (res) {

var re = res;

alert(re);

},

error: function (xmlRes, err, c) {

var xml = xmlRes;

alert(err);

}

});*/

//GET请求,类型为text

/*$.get(

"Response.ashx",

function (res) {

alert(res);

}

)*/

//GET请求,带参数类型为text

/*$.get(

"Response.ashx",

{ "param": "我是请求的传递参数" },

function (res) {

alert(res);

}

);*/

//GET请求,类型为xml

/*$.get(

"Response.ashx",

function (res) {

alert($(res).find("gonghui").text());

}

);*/

//Ajax的GET请求,类型为xml

/*$.ajax({

type: "GET",

url: "Response.ashx",

contentType: "application/xml;charset=utf-8;",

dataType: "xml",

success: function (res) {

var re = $(res).find("gonghui").text();

alert(re);

},

error: function (xmlRes, err, c) {

var xml = xmlRes;

alert(err);

}

});*/

//Ajax的POST请求,类型为json

$.ajax({

type: "POST",

url: "Response.ashx",

contentType: "application/json;charset=utf-8;",

dataType: "json",

success: function (res) {

var re = res;

alert(re);

},

error: function (xmlRes, err, c) {

var xml = xmlRes;

alert(err);

}

});

})

</script>

</head>

<body>

<form id="form1" runat="server">

<div>

</div>

</form>

</body>

</html>

后台Request.cs

[csharp] view
plain copy

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class Ashx_Request : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Session["sk"] = "我是请求页中请求的session数据";

}

}

ashx的代码:

[csharp] view
plain copy

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

using System;

using System.Web;

using System.Web.SessionState;

public class Response : IHttpHandler,IRequiresSessionState {

public void ProcessRequest (HttpContext context) {

//RequestSession(context);

//GetRequest(context);

//GetRequestParam(context);

//GetRequestXml(context);

RequestJson(context);

}

public bool IsReusable {

get {

return false;

}

}

private void RequestSession(HttpContext context)

{

context.Response.ContentType = "text/plain";

string sk = context.Session["sk"] as string;

context.Response.Write(sk);

}

private void GetRequest(HttpContext context)

{

context.Response.ContentType = "text/plain";

context.Response.Write("Hello World");

}

private void GetRequestParam(HttpContext context)

{

context.Response.ContentType = "text/plain";

string param = context.Request["param"] ?? "";

context.Response.Write("传递参数值为:" + param);

}

private void GetRequestXml(HttpContext context)

{

context.Response.ContentType = "application/xml";

context.Response.Write("<gonghui>Hello World</gonghui>");

}

private void RequestJson(HttpContext context)

{

context.Response.ContentType = "application/json";

context.Response.Write("[\"Hello World.\"]");

}

}

二、请求webservice方法

Request页面的关键代码:

[javascript] view
plain copy

$(function () {

//Ajax使用POST请求,传递参数

/*$.ajax({

type: "POST",

url: "WebService.asmx/GetMyName",

data: "{'name':'gonghui'}", //请求webservice方法的时候,必须这样传递参数,如果遇到单引号,注意转码。

//data:"name=acles", // 请求webservice方法的时候,不能这样传递参数,会报“无效的JSON基元的错误”

contentType: "application/json;charset=utf-8",

dataType: "json",

success: function (res) {

alert(res.d);

},

error: function (xmlRes, err, c) {

alert(err);

}

});*/

/*$.ajax({

type: "GET",

url: "WebService.asmx/GetXml",

contentType: "application/xml;charset=utf-8;",

dataType: "xml",

success: function (res) {

var my = $(res).find("gonghui").text();

alert(my);

}

});*/

$.ajax({

type: "POST",

url: "WebService.asmx/GetXmlPost",

contentType: "application/xml;charset=utf-8;",

dataType: "xml",

success: function (res) {

var my = $(res).find("string").text();

alert(my);

}

});

})

WebService的关键代码:

[csharp] view
plain copy

[WebMethod]

public string GetMyName(string name) {

return "这是WebService页面的值,这是Request传递的参数值:" + name;

}

[WebMethod]

[System.Web.Script.Services.ScriptMethod(UseHttpGet=true)]

public System.Xml.XmlDocument GetXml() {

string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><gonghui>我是通过Get方式请求返回的xml</gonghui>";

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

doc.LoadXml(xml);

return doc;

}

[WebMethod]

public string GetXmlPost() {

return "我是通过POST方式请求返回的XML";

}

三、请求Aspx页面的例子

Request页面的关键代码:

[javascript] view
plain copy

//直接请求页面的方法,其实请求本身页面和其他页面都是一样的,无非就是地址不一样

$(function () {

//请求自身页面

/*$.get(

"Request.aspx",

{ "token": "ajax" },

function (data) {

$("#dataShow").text(data);

}

);*/

//请求其它页面

/*$.ajax({

type: "GET",

url: "Response.aspx",

data: { token: "ajax" },

dataType: "json",

contentType: "application/json;charset=utf-8",

success: function (data) {

var arr = data;

$.each(arr, function (idx, item) {

alert(item.StudentID + "/" + item.Hometown);

});

},

error: function (d, c, e) {

var dd = d;

}

});*/

//请求自身页面方法(get方法),返回是自身页面的html源码

/*$.get(

"Request.aspx/RequestMethod",

function (data) {

$("#dataShow").text(data);

}

);*/

//请求页面,带参数

$.ajax({

type: "POST",

url: "Response.aspx/RequestMethod1",

data: "{'msg':'这是一个msg参数值'}",

contentType: "application/json;charset=utf-8", //这句话不要忘了

dataType: "json",

success: function (res) {

$("#dataShow").text(res.d);

},

error: function (xmlReq, err, c) {

$("#dataShow").text(err);

}

});

})

Response.cs的关键代码:

[csharp] view
plain copy

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

StreamReader reader = new StreamReader(Request.InputStream);

string dd = reader.ReadToEnd();

if ((Request["token"] ?? "") == "ajax")

{

//Response.ContentType = "application/Json";

//Response.ContentEncoding = System.Text.Encoding.UTF8;

Response.Clear();

Response.Write("[{\"StudentID\":1,\"Name\":\"tmac\",\"Hometown\":\"GuangDong\"},{\"StudentID\":2,\"Name\":\"gonghui\",\"Hometown\":\"HuNan\"}]");

Response.Flush();

Response.End();

}

//Response.Write(RequestMethod());

}

}

[System.Web.Services.WebMethod]

[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]

public static string RequestMethod()//方法一定是静态的

{

return "";

}

[System.Web.Services.WebMethod]

public static string RequestMethod1(string msg)

{

return "这是一个Response页面值,由Request页面的Ajax传入的值:" + msg;

}

本案例源代码的下载地址1:http://download.csdn.net/detail/lovegonghui/9173673

本案例源代码的下载地址2:http://down.51cto.com/data/2104468
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: