Jquery Ajax请求(1)

jQuery.ajax(options)请求

http://s.click.taobao.com/t_8?e=7HZ5x%2BOzdswsVvyc5Jts79Au1Q%3D%3D&p=mm_24156262_0_0

jQuery 底层 AJAX 实现。简单易用的高层实现见 $.get, $.post 等。

$.ajax() 返回其创建的 XMLHttpRequest 对象。大多数情况下你无需直接操作该对象,但特殊情况下可用于手动终止请求。

$.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数信息。详细参数选项见下。

 返回值:XMLHttpRequest

 参    数:是可以选的

选    项:async (Boolean) : (默认: true) 默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。

              注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。

              beforeSend (Function) : 发送请求前可修改 XMLHttpRequest 对象的函数,如添加自定义 HTTP 头。XMLHttpRequest 对象是唯一的参数。 Ajax 事件.

             cache (Boolean) : (默认: true,dataType为script时默认为false),设置为 false 将不会从浏览器缓存中加载请求信息。

            complete (Function) : 请求完成后回调函数 (请求成功或失败信息)。

            contentType (String) : (默认: "application/x-www-form-urlencoded") 发送信息至服务器时内容编码类型。默认值适合大多数应用场合。

            data (Object,String) : 发送到服务器的数据。

           dataFilter (Function) :给Ajax返回的原始数据的进行预处理的函数。

            dataType (String) : 预期服务器返回的数据类型。

                                            "xml": 返回 XML 文档,可用 jQuery 处理。

                                            "html": 返回纯文本 HTML 信息;包含 script 元素。

                                           "script": 返回纯文本 JavaScript 代码。不会自动缓存结果。除非设置了"cache"参数

                                           "json": 返回 JSON 数据 。

                                           "jsonp": JSONP 格式。使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。

                                           "text": 返回纯文本字符串

jQuery 代码:

加载并执行一个 JS 文件。

$.ajax({
  type: "GET",
  url: "test.js",
  dataType: "script"
});
保存数据到服务器,成功时显示信息。
$.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
});

装入一个 HTML 网页最新版本

$.ajax({
  url: "test.html",
  cache: false,
  success: function(html){
    $("#results").append(html);
  }
});

同步加载数据。发送请求时锁住浏览器。需要锁定用户交互操作时使用同步方式。

 var html = $.ajax({
  url: "some.php",
  async: false
 }).responseText;

事列:

JQuery代码:

 <script type="text/javascript" language="javascript">
        $().ready(function() {
            $.ajax({ type: "post", url: "JqueryAddData.ashx", success: function(msg) {
                $("#divshow").html(msg);
            }
            });
            $.ajax({ type: "post", url: "JqueryAddData.ashx", data: "name=John&age=32", success: function(msg) {
                alert(msg);
            }
        });
           
           
        });
    </script>

 

JqueryAddData.ashx (Code)

<%@ WebHandler Language="C#" class="JqueryAddData" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Text;

public class JqueryAddData : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        context.Response.Write(SelectAdd());
        string name = HttpContext.Current.Request.Params["name"].ToString();
        string age = HttpContext.Current.Request.Params["age"].ToString();
        context.Response.Write(InsertDataTable(name, age));
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public string SelectAdd()
    {
        string sql = "select fileid,[filename] from sysfiles";
        string connString = @"Data Source=VANSKY-34F1\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
        StringBuilder builder = new StringBuilder();
        builder.Append("<table>");
        builder.Append("<tr><td>文件名</td><td>路径</td></tr>");
        using (SqlConnection connection = new SqlConnection(connString))
        {
            connection.Open();
            SqlCommand command = new SqlCommand(sql, connection);
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            while (reader.Read())
            {
                builder.Append("<tr><td>" + reader[1].ToString() + "</td><td></td></tr>");
            }
            connection.Close();
            builder.Append("</table>");
            return builder.ToString();
        }

    }

    public string InsertDataTable(string name, string age)
    {
        string sql = "insert into DataTable values(@Name,@Age)";
        string connString = @"Data Source=VANSKY-34F1\SQLEXPRESS;Initial Catalog=DBTest;Integrated Security=True";
        using (SqlConnection connection = new SqlConnection(connString))
        {
            connection.Open();
            SqlCommand command = new SqlCommand(sql, connection);
            command.Parameters.AddWithValue("@Name", name);
            command.Parameters.AddWithValue("@Age", age);
            int reader = command.ExecuteNonQuery();
            if (reader > 0)
            {
                return "成功!";
            }
            else
            {
                return "失败!";
            }
        }

    }
   
}http://s.click.taobao.com/t_8?e=7HZ5x%2BOzdswsVvyc5Jts79Au1Q%3D%3D&p=mm_24156262_0_0

posted @ 2011-01-04 15:28  向——丁——丁  阅读(1627)  评论(0编辑  收藏  举报