博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Jquery异步请求数据实例
阅读量:4322 次
发布时间:2019-06-06

本文共 2379 字,大约阅读时间需要 7 分钟。

一、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代码:

ExpandedBlockStart.gif
View Code
    
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代码

ExpandedBlockStart.gif
View Code
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);
                    }
                });
            });

后台代码:

ExpandedBlockStart.gif
View Code
<%@ 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;
        }
    }
}

转载于:https://www.cnblogs.com/Xingsoft-555/archive/2011/12/27/2304030.html

你可能感兴趣的文章
原生js大总结二
查看>>
PHP基础
查看>>
UVa 11488 超级前缀集合(Trie的应用)
查看>>
Django 翻译与 LANGUAGE_CODE
查看>>
[转]iOS教程:SQLite的创建数据库,表,插入查看数据
查看>>
【转载】OmniGraffle (一)从工具栏开始
查看>>
初识ionic
查看>>
java 中打印调用栈
查看>>
开发 笔记
查看>>
数据挖掘算法比赛 - 简单经验总结
查看>>
生成商户订单号/退款单号
查看>>
使用Android OpenGL ES 2.0绘图之六:响应触摸事件
查看>>
我们过去几年做对了哪些事
查看>>
ubuntu 16.04LTS
查看>>
javascript深入理解js闭包
查看>>
Oracle的安装
查看>>
Android Socket连接PC出错问题及解决
查看>>
Android Studio-—使用OpenCV的配置方法和demo以及开发过程中遇到的问题解决
查看>>
第2天线性表链式存储
查看>>
python自动化测试-D11-学习笔记之一(yaml文件,ddt)
查看>>