0

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;
                }   
            }
         } 
1
  • What have you tried and what problems did you run into? Noone* is going to want to do your work for you. Commented Aug 22, 2012 at 17:03

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thanks for your help. The code works with Firefox and Chrome but not with IE 8. Any idea why?
@Joly What is not working exactly? Do you get an error? You can try Fiddler fiddler2.com/fiddler2 to see what goes over the wire. Any clues would be useful. jQuery version?
jQuery version: 1.8.0. I just noticed something strange: If I use my code above then it works with IE8 and doesn't work with FF 14.0 and Chrome 21.0 due to same origin policy ("is not allowed by Access-Control-Allow-Origin" error). If I use your code then it doesn't work in IE or FF (no error is given) and not in Chrome where again I'm getting the same error as with my code: XMLHttpRequest cannot load http://.... Origin http://... is not allowed by Access-Control-Allow-Origin.
OK fixed by adding Access-Control-Allow-Origin: * on the other side
1
$.ajax({
  type: 'GET', // get by default anyway
  url: 'http://www.my.com',
  contentType: 'your/header',
  success: function(data){
    $('#responseText').html(data);
  }
});

Comments

0

Look at the API. Should be something like:

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.