需要帮助将AJAX代码转换为jQuery

时间:2012-08-22 17:01:42

标签: jquery

有人可以帮助我将下面的代码转换为jQuery吗?

var xmlhttp;

            if (window.XMLHttpRequest)
            {
                // Code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else
            {
                // Code for IE5, IE6
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.open("GET", "http://www.my.com", true);
            xmlhttp.setRequestHeader("MyHeader", "hello");
            xmlhttp.send();

            xmlhttp.onreadystatechange = function()
            {
                if (xmlhttp.readyState == 4)
                {
                    document.getElementById("responseText").innerHTML = xmlhttp.responseText;
                }   
            }
         } 

4 个答案:

答案 0 :(得分:2)

简单的例子......

$.ajax({
  "type": "get", // optional
  "url": "http://www.my.com",
  "headers": { "MyHeader": "hello" },
  "success": function (data) {
    document.getElementById("responseText").innerHTML = data;
  }
});

有关更多选项,请参阅documentation

答案 1 :(得分:1)

$.ajax({
  type: 'GET', // get by default anyway
  url: 'http://www.my.com',
  contentType: 'your/header',
  success: function(data){
    $('#responseText').html(data);
  }
});

答案 2 :(得分:0)

Look at the API。应该是这样的:

$.ajax({
  url: 'http://www.my.com',
  headers: {
     "key": "value"
  },
  onSuccess: function(data){
     $('#responseText').html(data);
});

答案 3 :(得分:0)

对不起,我不是AJAX专家,但看起来你正在尝试发出GET请求并阅读回复。 如果是这样 - 您需要执行以下操作:

$.get('http://www.my.com/page.php?variable1=value1', function(data){
   $('#responseText').html(data);
})

类似的东西。

相关问题