现在的位置: 首页 > 综合 > 正文

jQuery ajax – post() 方法

2017年11月06日 ⁄ 综合 ⁄ 共 2435字 ⁄ 字号 评论关闭

 
清单 4. 使用 Ajax 向页面发送数据

                
$.post('save.cgi', {
    text: 'my string',
    number: 23
}, function() {
    alert('Your data has been saved.');
});


如果您确实需要编写一些复杂的 Ajax 脚本,那么需要用到 $.ajax() 函数。您可以指定 xmlscripthtml 或者 json,jQuery
将自动为回调函数准备合适的结果,这样您便可以立即使用该结果。还可以指定 beforeSenderrorsuccess 或者 complete 回调函数,向用户提供更多有关
Ajax 体验的反馈。此外,还有一些其它的参数可供使用,您可以使用它们设置 Ajax 请求的超时,也可以设置页面 “最近一次修改” 的状态。清单 5 显示了一个使用一些我所提到的参数检索 XML 文档的示例。


清单 5. $.ajax() 使 Ajax 由复杂变简单 

                
$.ajax({
    url: 'document.xml',
    type: 'GET',
    dataType: 'xml',
    timeout: 1000,
    error: function(){
        alert('Error loading XML document');
    },
    success: function(xml){
        // do something with xml
    }
});

定义和用法

post() 方法通过 HTTP POST 请求从服务器载入数据。

语法

jQuery.post(url,data,success(data, textStatus, jqXHR),dataType)
参数 描述
url 必需。规定把请求发送到哪个 URL。
data 可选。映射或字符串值。规定连同请求发送到服务器的数据。
success(data, textStatus, jqXHR) 可选。请求成功时执行的回调函数。
dataType

可选。规定预期的服务器响应的数据类型。

默认执行智能判断(xml、json、script 或 html)。

详细说明

该函数是简写的 Ajax 函数,等价于:

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

根据响应的不同的 MIME 类型,传递给 success 回调函数的返回数据也有所不同,这些数据可以是 XML 根元素、文本字符串、JavaScript 文件或者 JSON 对象。也可向 success 回调函数传递响应的文本状态。

对于 jQuery 1.5,也可以向 success 回调函数传递 jqXHR 对象(jQuery 1.4 中传递的是
XMLHttpRequest 对象)。

大部分实现会规定一个 success 函数:

$.post("ajax/test.html", function(data) {
  $(".result").html(data);
});

本例读取被请求的 HTML 片段,并插入页面中。

通过 POST 读取的页面不被缓存,因此 jQuery.ajaxSetup() 中的
cache 和 ifModified 选项不会影响这些请求。

注释:由于浏览器安全方面的限制,大多数 "Ajax" 请求遵守同源策略;请求无法从不同的域、子域或协议成功地取回数据。

注释:如果由 jQuery.post() 发起的请求返回错误代码,那么不会有任何提示,除非脚本已调用了全局的 .ajaxError()
方法
。或者对于 jQuery 1.5,jQuery.post() 返回的 jqXHR 对象的 .error() 方法也可以用于错误处理。

实例

$.ajax({

        //添加、修改方法
        type: "post", //使用get方法访问后台
        dataType: "text", //返回json
        async: true, //true 异步  false为同步请求 
        url: "AddProduct.ashx?typeid=5&productid=" + productid, //要访问的后台地址 
        data: {
            productname: _productName,
            price0: _price0,
            price1: _price1       
        },
        success: function (msg) { //msg为返回的数据,在这里做数据绑定
            if (msg == "1") {                
                location.href = "/Product/ProductList.aspx";
            }
            else {
                ShowMessageError("操作失败");
               
            }
        }

    });

对方页面这样接收数据

   string productid = context.Request.QueryString["productid"];

string productname= context.Request.Params["productname"];


将结果json转换为对象

 $.ajax({
        type: "get", //使用get方法访问后台
        dataType: "text", //返回json
        async: false, //true 异步  false为同步请求 
        url: "AddProduct.ashx?typeid=1&catepid=" + pid, //要访问的后台地址 
        success: function (msg) { //msg为返回的数据,在这里做数据绑定

           msg = eval("(" + msg + ")");//将结果json转换为对象


            if (msg != "" && msg != null && msg.length > 0) {

                for (var i = 0; i < msg.length; i++) {
               ...

               ...
                    }
}

});

});

                 
            

【上篇】
【下篇】

抱歉!评论已关闭.