Joly
Joly

Reputation: 3276

Need help converting AJAX code to jQuery

Can someone please help me convert the code below to 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;
                }   
            }
         } 

Upvotes: 0

Views: 109

Answers (4)

Split Your Infinity
Split Your Infinity

Reputation: 4229

Simple example...

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

See documentation for more options.

Upvotes: 2

Ohgodwhy
Ohgodwhy

Reputation: 50787

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

Upvotes: 1

John Smith
John Smith

Reputation: 921

I'm sorry I'm not an AJAX expert, but it looks like you're trying to make a GET request and read a response. If so - you need to do following:

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

Something like that.

Upvotes: 0

hvgotcodes
hvgotcodes

Reputation: 120198

Look at the API. Should be something like:

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

Upvotes: 0

Related Questions