将XMLHTTPRequest转换为$ ajax [英] converting XMLHTTPRequest into $ajax

查看:80
本文介绍了将XMLHTTPRequest转换为$ ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码在小工具中抓取Google:

I am fetching Google in a gadget using this code:

<html>
<head>
<title>Browser</title>
</head>
<body onload= "getListCollection()" style="width:900px; height:900px;" >
<div id="listAttributes" style="width:400px; height:400px;" ></div>
<script>

function getListCollection() {
    var url = "https://www.google.co.uk"  
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open('GET',url,true, 'user', 'password');
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState == 4);
        {
            document.getElementById("listAttributes").innerHTML = xmlhttp.responseText
        }
    }
    xmlhttp.send(null);
}
</script>
</body>
</html>

我想知道如何使用$ ajax做同样的事情?

I wonder how can I do the same thing using $ajax?

我看了不同的例子,但我不知道他们将如何适应这种情况。或者,如果你可以发布一些不错的$ ajax教程,请。

I looked at different examples but I don't know how they gonna fit in this situation. Or if you can post some nice $ajax tutorials please.

当我将其更改为此时,它不起作用:

When I change it to this, it doesn't work:

<html>
<head>
<title>Browser</title>
</head>
<body onload= "getListCollection()" style="width:900px; height:900px;" >
<div id="listAttributes" style="width:400px; height:400px;" ></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js">

$.ajax({
    url : 'https://www.google.co.uk',
    type : 'GET',
    success : function(data){
        $('#listAttributes').html(data);
    }
});
</script>
</body>
</html>

页面上没有任何内容。

推荐答案

这是代码的jQuery版本:

Here's a jQuery version of the code:

$.ajax({
    url : 'https://www.google.co.uk', //cross-domain? this won't work
    username : 'user',
    password : 'password',
    type : 'GET', //default is GET, but i put it here to clarify
    success : function(data){
        $('#listAttributes').html(data);
    }
});

有关更多详细信息和设置,请阅读 jQuery.ajax()文档

For more details and settings, read the jQuery.ajax() documentation

这篇关于将XMLHTTPRequest转换为$ ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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