Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I replace this JQuery Ajax call with JavaScript's builtin functions? [duplicate]

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));
    }
});
like image 800
pencilCake Avatar asked Sep 15 '25 00:09

pencilCake


2 Answers

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:

  • Reqwest
  • Fermata
  • Ajax (inspired by jquery/zepto)
  • Micro Ajax
like image 97
Johnny Avatar answered Sep 17 '25 14:09

Johnny


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;
    }
  }
like image 29
GajendraSinghParihar Avatar answered Sep 17 '25 13:09

GajendraSinghParihar