zoukankan      html  css  js  c++  java
  • jQuery $.post $.ajax用法

    jQuery.post( url, [data], [callback], [type] ) :使用POST方式来进行异步请求

    参数:

    url (String) : 发送请求的URL地址.

    data (Map) : (可选) 要发送给服务器的数据,以 Key/value 的键值对形式表示。

    callback (Function) : (可选) 载入成功时回调函数(只有当Response的返回状态是success才是调用该方法)。

    type (String) : (可选)官方的说明是:Type of data to be sent。其实应该为客户端请求的类型(JSON,XML,等等)

    这是一个简单的 POST 请求功能以取代复杂 $.ajax 。请求成功时可调用回调函数。如果需要在出错时执行函数,请使用 $.ajax。示例代码:

    Ajax.aspx:

    Response.ContentType = "application/json";Response.Write("{result: '" + Request["Name"+ ",你好!(这消息来自服务器)'}");

    jQuery 代码:

    $.post("Ajax.aspx", { Action: "post", Name: "lulu" },     
       function (data, textStatus){        
        // data 可以是 xmlDoc, jsonObj, html, text, 等等.  
        //this;
        // 这个Ajax请求的选项配置信息,请参考jQuery.get()说到的this  
       alert(data.result);        }, "json");

    点击提交:

    这里设置了请求的格式为"json":


    $.ajax()这个是jQuery 的底层 AJAX 实现。简单易用的高层实现见 $.get, $.post 等。

    这里有几个Ajax事件参数:beforeSend success complete ,error 。我们可以定义这些事件来很好的处理我们的每一次的Ajax请求。

    复制代码
    $.ajax({
    url: 'stat.php',

    type: 'POST',

    data:{Name:"keyun"},

    dataType: 'html',

    timeout: 1000,

    error: function(){alert('Error loading PHP document');},

    success: function(result){alert(result);}

    });
    复制代码

    //add by Q at 2008.11.25

    今天遇到一个jquery的post的小问题

    因为要批量删除,所以开始用循环的post到那个url,然后刷新本页

    这就出现问题了

    复制代码
    $("input[@name='qa_checkbox']").each(function()
    {
        if($(this).attr('checked'== undefined)
        {
                    
        }
        else
        {
            $.post(url,{Action:"POST"},function(data){alert(data);window.location.reload();}, "text");
                    
        }
    })
    复制代码


    这么用的话 只能删除第一条数据;

    复制代码
    $("input[@name='qa_checkbox']").each(function()
    {
        if($(this).attr('checked'== undefined)
        {
                    
        }
        else
        {
            $.post(url+$(this).val(),{Action:"POST"},function(data){alert(data);}, "text");
                    
        }
    })

    window.location.reload();
    复制代码

    这样用的话,虽然可以删除,也能刷新本页,貌似reload是在post的function之前运行,但是post会报错,其中原因有待研究;

    最终想了折中的办法 

    复制代码
    $("input[@name='qa_checkbox']").each(function()
            {
                if($(this).attr('checked'== undefined)
                {
                    
                }
                else
                {
                    url = url + $(this).val() + '_';
                    
                }
            })
            $.post(url,{Action:"POST"},function(data){alert(data);window.location.reload();}, "text");
        }
    复制代码
     

     把要删除的id连成字符串,用一次post处理,把reload放在post的function里 就没有问题了

    项目实例:

    function clearSys() {
        $.dialog.confirm('你确认删除操作?', function(){
            $.post("{:getUrl('ucentermsg/clearsys')}",function(data){
            if (!data) {
                Kshop.optFail();
                return false;
            }
            if (data.status == 1){
                setTimeout('window.location.href=window.location.href;',1000);
                Kshop.optDone(data.info);    
            }else{
                Kshop.optFail(data.info);
            }    
        }, 'json');
        });
    }

    后台:

     public function clearSys()
        {
            $msg = D('Message');
            $result = $msg->saveAllFlags($this->user_info['uid'], $msg::SYS_TO_UID);
            if ($result) {
                $this->ajaxReturn('', '删除成功!', 1);
            } else {
                $this->ajaxReturn('', '删除失败!', 0);
            }
        }

     protected function ajaxReturn($data,$info='',$status=1,$type='') {
            $result  =  array();
            $result['status']  =  $status;
            $result['info'] =  $info;
            $result['data'] = $data;
            //扩展ajax返回数据, 在Action中定义function ajaxAssign(&$result){} 方法 扩展ajax返回数据。
            if(method_exists($this,"ajaxAssign"))
                $this->ajaxAssign($result);
            if(empty($type)) $type  =   C('DEFAULT_AJAX_RETURN');
            if(strtoupper($type)=='JSON') {
                // 返回JSON数据格式到客户端 包含状态信息
                header("Content-Type:text/html; charset=utf-8");
                exit(json_encode($result,JSON_HEX_TAG));
            }elseif(strtoupper($type)=='XML'){
                // 返回xml格式数据
                header("Content-Type:text/xml; charset=utf-8");
                exit(xml_encode($result));
            }elseif(strtoupper($type)=='EVAL'){
                // 返回可执行的js脚本
                header("Content-Type:text/html; charset=utf-8");
                exit($data);
            }else{
                // TODO 增加其它格式
            }
        }

  • 相关阅读:
    【leetcode】Validate Binary Search Tree
    【leetcode】Add Binary
    【leetcode】Search a 2D Matrix
    绑定方法与非绑定方法||反射||内置方法
    封装||property
    组合||抽象类||多态||鸭子类型
    在子类中重用父类的属性
    继承||派生||继承实现原理
    面向对象小练习
    面向对象编程
  • 原文地址:https://www.cnblogs.com/tangyangping/p/3993282.html
Copyright © 2011-2022 走看看