如何使用Ajax使用jQuery如何使用、Ajax、jQuery

2023-09-10 16:36:28 作者:导演,给我的人生来次NG

 函数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");
   }
如何用ajax jquery 清除image src

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!");
    }
});