本文引用自Xingsoft

一、Jquery向aspx页面请求数据

前台页面JS代码:


            $("#Button1").bind("click", function () {
                $.ajax({
                    type: "post",
                    url: "default.aspx",
                    data: "name=" + $("#Text1").val(),
                    success: function (result) {
                        alert(result.msg);
                    }
                });
            });
 
<input id="Text1" type="text" value='张三'/>        
<input id="Button1" type="button"  value="提交" />

后台cs代码:

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

二、Jquery向WebService页面请求数据


           $("#Button2").bind("click", function () {
                $.ajax({
                    type: "post",
                    contentType: "application/json",
                    url: "WebService.asmx/HelloWorld",
                    data: "{name:'" + $("#Text1").val() + "'}",
                    datatype: "json",
                    success: function (result) {
                        alert(result.d);
                    }
                });
            }); <input id="Button2" type="button"  value="向WebService提交" />

WebService代码

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

///<summary>
/// Summary description for WebService
///</summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

public WebService () {

//Uncomment the following line if using designed components
        //InitializeComponent();
    }

[WebMethod]
    public string HelloWorld( string name) {
        return "Hello World"+name;
    }
   
}

三、Jquery向ashx请求数据和向页面相同

Js代码:


            $("#Button3").bind("click", function () {
                $.ajax({
                    type: "post",
                    url: "Handler.ashx",
                    data: "name=" + $("#Text1").val(),
                    success: function (result) {
                        alert(result.msg);
                    }
                });
            });

后台代码:

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

using System;
using System.Web;

public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/json";
        context.Response.Write("{\"msg\":\"Hello World"+context.Request["name"]+"来自handler.ashx\"}");
        context.Response.End();
    }

public bool IsReusable {
        get {
            return false;
        }
    }

}

Jquery异步请求简单实例(转)的更多相关文章

  1. Jquery异步请求数据实例

    一.Jquery向aspx页面请求数据 前台页面JS代码: $("#Button1").bind("click", function () { $.ajax({ ...

  2. Jquery异步请求数据实例代码

    一.Jquery向aspx页面请求数据 前台页面JS代码: 代码如下: $("#Button1").bind("click", function () { $. ...

  3. jQuery异步请求(如getJSON)跨域解决方案

    相信大家在使用jQuery异步请求非自己网站内相对资源(通过别人站点上的URL直接读取)使经常会遇到如下错误吧,实际上这些错误都是浏览器安全机制“搞的鬼”,才让我们开发路上遇到了拦路虎. 当你直接在浏 ...

  4. jQuery异步请求模拟IE登录网站

    具体请求的登录验证页面后台逻辑处理,这里我们忽略,不在我们的学习范围内:关键的是使用jQuery异步请求方法,如下例子: <%@ Page Language="C#" Aut ...

  5. 实现在Android简单封装类似JQuery异步请求

    在android开发中经常会使用异步请求数据,通常会使用handler或者AsyncTask去做,handler 配合message 使用起来比较麻烦,AsyncTask 线程池只允许128个线程工作 ...

  6. Android简单封装类似JQuery异步请求

    在android开发中经常会使用异步请求数据,通常会使用handler或者AsyncTask去做,handler 配合message 使用起来比较麻烦,AsyncTask 线程池只允许128个线程工作 ...

  7. Ajax异步请求-简单模版

    <script type="text/javascript"> window.onload = function () { document.getElementByI ...

  8. jquery 之ajax,get,post异步请求简单代码模版

    $.get(                        "../cart/cart_list.do", "productId="+productId, fu ...

  9. jQuery异步请求ajax()之complete参数详解

    请求完成后回调函数 (请求success 和 error之后均调用).这个回调函数得到2个参数:XMLHTTPRequest) 对象和一个描述请求状态的字符串("success", ...

随机推荐

  1. [改善Java代码]推荐在复杂字符串操作中使用正则表达式

    一.分析  字符串的操作,诸如追加.合并.替换.倒序.分隔等,都是在编码过程中经常用到的,而且Java也提供了append.replace.reverse.split等方法来完成这些操作,它们使用起来 ...

  2. [改善Java代码]注意Class类的特殊性

    Java语言是先把Java源文件编译成后缀为class的字节码文件,然后再通过ClassLoader机制把这些类文件加载到内存中,最后生成实例执行的,这是Java处理的基本机制,但加载到内存中的数据是 ...

  3. poj 2186 强连通分支 和 spfa

    思路: 建图时,分别建正向图edge和转置图T.用正向图edge来DFS,找出第一个被发现的强连通分支(如果该图存在题目要求的点,那么一定就是第一个被发现的).然后用spfa跑转置图T,判断被发现的点 ...

  4. 织梦dedecms源码安装方法

    织梦dedecms源码安装方法 第一步: 上传所有文件到空间 注意:(由于有很多人反应安装后首页样式都乱的,所以强烈要求安装到根目录,如:127.0.0.1 / www.xxx.com,或者二级域名也 ...

  5. Nginx - Rewrite Module

    Initially, the purpose of this module (as the name suggests) is to perform URL rewriting. This mecha ...

  6. 使用spring+hibernate+atomikos+tomcat构建分布式事务

    本文通过一个demo,介绍如何使用spring+hibernate+atomikos+tomcat构建在一个事务中涉及两个数据源的web应用. demo功能:实现一个能成功提交和回滚的涉及两个数据库数 ...

  7. 上传系列:jquery.upload.js

    最近想做一个上传的总结,把自己用过的上传插件都写一写,哪天用到的时候就不用再一次的翻阅资料,现在页面上用到的上传插件,原理上都差不多,那我也就不再废话了,下面我主要记录一下几个比较常用的,由简到繁,这 ...

  8. 并行执行的Service,以媒体转码成新格式为例

    大家众所周知,IntentService内置的handler只有一个线程,而AsyncTask又只适合时间至多几秒的操作,所以我们关注使用ExecutorService建立并行执行.为了确保Servi ...

  9. Apache httpd开启SSL

    https://my.oschina.net/xpbug/blog/197454 必要软件 apache httpd 2.4(windows) perl 5 (windows) openssl (wi ...

  10. linux c/c++ IP字符串转换成可比较大小的数字

    由www.169it.com搜集整理 IP字符串转换成可比较大小的数字,具体代码如下所示: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include "stdio. ...