0

Are there any libraries that support creation of the post data for XMLHTTPRequest in javascript. consider the sending of post data as follows.

var http = new XMLHttpRequest();
var url = "get_data.php";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(params);

Send POST data using XMLHttpRequest

I am wondering if there is a better way to assembling the data in the form(as opposed to string concatenation). var params = "lorem=ipsum&name=binny";

Community
  • 1
  • 1
Pradyot
  • 2,349
  • 7
  • 35
  • 49

2 Answers2

1

What about JQuery.param()? See http://api.jquery.com/jquery.param/.

var params = { 
   lorem:ipsum, 
   name:binny 
};
var str = jQuery.param( params );
// str = lorem=ipsum&name=binny

Another, this time, vanilla JavaScript solution would come from this post.

Community
  • 1
  • 1
guysigner
  • 2,594
  • 1
  • 15
  • 23
0

Check out qwest simple, efficient and weights only 8.5K

vorillaz
  • 5,285
  • 2
  • 24
  • 41