4

Possible Duplicate:
javascript ajax request without framework

How can I make the JQuery Ajax call below without using JQuery or any other library but a by only using JavaScript built-in functions?

var input = '{
    "age":100,
    "name":"foo",
    "messages":["msg 1","msg 2","msg 3"],
    "favoriteColor" : "blue",
    "petName" : "Godzilla",
    "IQ" : "QuiteLow"
}';
var endpointAddress = "http://your.server.com/app/service.svc";
var url = endpointAddress + "/FindPerson";
$.ajax({
    type: 'POST',
    url: url,
    contentType: 'application/json',
    data: input,
    success: function(result) {
        alert(JSON.stringify(result));
    }
});
3
  • 1
    I really do have to ask ... Why? Commented Dec 18, 2012 at 13:30
  • 1
    MDN has a wonderful write-up on AJAX and what jQuery and other libraries do in the background for you. Commented Dec 18, 2012 at 13:30
  • What have you tried? Even Google finds the answer without anyone else having to copy/paste it for you. Commented Dec 18, 2012 at 13:36

4 Answers 4

3

jQuery does a good job normalizing all the little quirks and nuonces between browsers for ajax calls.

I'd suggest finding a stand-alone ajax library that can do the same thing but without all the extra overhead jQuery brings with it. Here are a few:

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

1 Comment

axios seems also to be promising nowadays
2

Try this Example

First You have to create object of window.XMLHttpRequest or ActiveXObject (for IE)

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

Then You can send the request

xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();

At last You can Get the responce

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

Comments

2

The code below does everything that your jQuery version does:

  • POST request with JSON as postdata
  • Sets the JSON Content-type header
  • Alerts the stringified response

Code:

  var httpRequest;

  function makeRequest(url, input) {
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
      httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
      try {
        httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e) {
        try {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {}
      }
    }

    if (!httpRequest) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
    }
    httpRequest.onreadystatechange = function(){
        if (httpRequest.readyState === 4) {
          if (httpRequest.status === 200) {
            alert(JSON.stringify(httpRequest.responseText));
          }
        }
    };

    httpRequest.open('POST', url);
    httpRequest.setRequestHeader('Content-Type', 'application/json');
    httpRequest.send(input);
  }

var input = '{
    "age":100,
    "name":"foo",
    "messages":["msg 1","msg 2","msg 3"],
    "favoriteColor" : "blue",
    "petName" : "Godzilla",
    "IQ" : "QuiteLow"
}';
var endpointAddress = "http://your.server.com/app/service.svc";
var url = endpointAddress + "/FindPerson";
makeRequest(url, input);

Taken partly from MDN.

Comments

-1

Take a look at Google's first answer over here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.