如何使用Ajax使用jQuery [英] How to use Ajax with jQuery

查看:119
本文介绍了如何使用Ajax使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数ajaxFunction(){

    VAR ajaxRequest; //让Ajax成为可能的变量!

尝试{
        //歌剧8.0+,火狐,Safari浏览器
        ajaxRequest =新XMLHtt prequest();
    }赶上(五){
        // Internet Explorer浏览器
        尝试{
            ajaxRequest =新的ActiveXObject(MSXML2.XMLHTTP);
        }赶上(五){
            尝试{
                ajaxRequest =新的ActiveXObject(Microsoft.XMLHTTP);
            }赶上(五){
                // 出了些问题
                警报(您的浏览器打破了!);
                返回false;
            }
        }
    }

    ajaxRequest.onreadystatechange =功能(){
        如果(ajaxRequest.readyState == 4和&安培; ajaxRequest.status == 200){
        警报(ajaxRequest.responseText);
        }
    }


    VAR TXT =的document.getElementById(数据);
    ajaxRequest.open(POST,hello.php扩展,真正的);

    ajaxRequest.send(用户=+ txt.value);
        警报(这里);
   }
 

我怎么能写完全相同的功能jQuery中的Ajax?

解决方案

  VAR TXT = $('#数据)VAL()。

$阿贾克斯({
    网址:hello.php,
    类型:'后',
    数据:{用户:TXT},
    成功:功能(数据){
        警报(数据);
    },
    错误:函数(ERR,REQ){
        警报(您的浏览器打破了!);
    }
});
 

function ajaxFunction(){

    var ajaxRequest;  // The variable that makes Ajax possible!

try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }

    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4 && ajaxRequest.status==200){
        alert(ajaxRequest.responseText);
        }
    }


    var txt = document.getElementById("data");
    ajaxRequest.open("POST", "hello.php", true);

    ajaxRequest.send("user=" + txt.value); 
        alert("here");
   }

How can I write the exact same functionality in jQuery Ajax?

解决方案

var txt = $('#data').val();

$.ajax({
    url: 'hello.php',
    type: 'post',
    data  : { user: txt }, 
    success: function(data) {
        alert(data);
    },
    error : function(err, req) {
        alert("Your browser broke!");
    }
});

这篇关于如何使用Ajax使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆