开发者

Lightweight AJAX objects recommendations? [closed]

开发者 https://www.devze.com 2023-02-06 18:36 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.

开发者_运维技巧

Closed 9 years ago.

Improve this question

I'm building a custom widget and i'm not supposed to use any framework such as jQuery, i need a lightweight cross browser object that supports POST & GET with callback, i've seen some solutions online but they include a lot of useless stuffs.

Thanks


Just use the XMLHttpRequest object yourself.

I tend to like looking at the Mozilla site to see examples, and they have a nice explanation on this subject.

Using XMLHttpRequest

It isn't hard to do, and it supports any HTTP request, and is as simple or complicated as you want to make it, depending on how easy you want to make it to use.

jQuery has a nice API though, so copying their style may be useful.

UPDATE:

You should reference the link above, but to see how POST can be done, mainly you put the parameters into the send function you can look at this. Don't just copy this code, I put it here for just a simple example.

var xhr = new XMLHttpRequest();
xhr.open("POST", "http://foo.com/submitform.php");
xhr.send(formData);

For GET you just put them in the URL, as you would in your address bar, and a null in the send.


If you can't use JQuery, you can write one from scratch without using any library:-

var xmlhttp;

if (window.XMLHttpRequest) {
  xmlhttp=new XMLHttpRequest();
}
else {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

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

// change "GET" to "POST" if you want to POST 
xmlhttp.open("GET","/yourlink",true);
xmlhttp.send();
0

精彩评论

暂无评论...
验证码 换一张
取 消