JQuery中ajax,get方法在ashx,aspx,asmx中的使用
来源:互联网 发布:软件开发全面质量管理 编辑:程序博客网 时间:2024/06/10 06:11
一、请求ashx的例子:
<%@ 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
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的代码:
<%@ 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页面的关键代码:
$(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的关键代码:
[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页面的关键代码:
//直接请求页面的方法,其实请求本身页面和其他页面都是一样的,无非就是地址不一样 $(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的关键代码:
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
0 0
- JQuery中ajax,get方法在ashx,aspx,asmx中的使用
- JQuery中ajax,get方法在ashx,aspx,asmx中的使用
- JQuery中ajax,get方法在asmx中的使用
- 续 .net3.5 C# Jquery+Ajax+Aspx(ashx/asmx)
- Jquery Ajax 登录,服务端分别为 aspx,ashx,asmx
- 转】[ASP.NET]使用jQuery的Ajax存取資料(ashx,aspx,asmx)
- jQuery.ajax 调用 服务(.aspx,.asmx)
- .net中Jquery ajax调用aspx和ashx文件
- 甩掉 ashx/asmx,使用jQuery.ajaxWebService请求WebMethod,Ajax处理更加简练
- 甩掉 ashx/asmx,使用jQuery.ajaxWebService请求WebMethod,Ajax处理更加简练
- 甩掉 ashx/asmx,使用jQuery.ajaxWebService请求WebMethod,Ajax处理更加简练
- 甩掉ashx/asmx,使用jQuery.ajaxWebService请求WebMethod,Ajax处理更加简练
- No 138 · 甩掉ashx/asmx,使用jQuery.ajaxWebService请求WebMethod,Ajax处理更加简练
- .NET中一般处理程序(ashx)在Ajax中的使用
- .NET中一般处理程序(ashx)在Ajax中的使用
- ajax 请求ashx或asmx
- .NET:在ASPX、ASHX和MVC中使用IOC容器
- Difference between .asmx, .aspx and .ashx
- IBM Watson 问答流程简介
- Java基础:接口和抽象类
- Android开机流程跟踪
- PHPStorm下XDebug配置
- 工程目录
- JQuery中ajax,get方法在ashx,aspx,asmx中的使用
- OCIlib的几个函数的执行效率(附上pro*c的性能对比)
- 在Windows的CMD中如何设置支持UTF8编码?
- linux下Mysql5.6源码安装
- IOS开发中的几种设计模式介绍
- js 页面效果 亮红灯泡
- javascript 如何遍历unicode字符集
- /usr/bin/ld:cannot open output file:Is a directory
- SQLite的简单介绍