I have a Java snippet code already implemented as follows:
//some logic above
JSONObject json = new JSONObject();
try {
json.put("name", "myname");
json.put("address","myaddress");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs = Utils.addEmpName(nameValuePairs);
nameValuePairs = Utils.addEmpAddress(nameValuePairs);
nameValuePairs.add(new BasicNameValuePair(
"myjsondata",
jsondata.toString());
....
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
It does a POST request to my server and sends the JSON data.
My question is how do I perform the same in Javascript. I know how to construct the JSON data but as far as nameValuePairs are concerned, can anyone tell me how to send them as my JSON body via javascript?
My Javascript code looks like this at present in Javascript
var data= { "name":"myname", "address" : "myaddress" };
var xhr = new XMLHttpRequest();
xhr.open("POST",my_url,true);
xhr.setRequestHeader("Content-type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(data));
I need help in writing the nameValuePairs part in order to construct json body