Javascript: how to send a request at rest

Hi, I want to ask you something how to send a request to webservice (restful) if the parameters that I will send to more than one parameter?

change

this.sendRequest = function(){
   var url="http://localhost:8081/inlinetrans/";

   var client = new XMLHttpRequest();
   var oriText ="";
   var stemText ="";
   var folText ="";

   client.open("PUT", url, false);

   client.setRequestHeader("Content-Type", "text/plain");
   client.send(oriText,stemText,folText);


   if (client.status == 200){
   client.responseText;
   }
   else{
    client.statusText;
   }

  }

client.send -> content parameters that I want to send to the server

+3
source share
1 answer

If you are making a data request, you should use a GET request. Any parameters necessary to obtain the correct data must be passed in the query string:

var url = 'http://localhost:8081/inlinetrans?key1=value1&key2=value2...';
client.open("GET", url, true);
client.send(null); 

If on the other hand you want to send data to the server, you should use the POST request:

var data = ....
client.open("POST", url, true);
client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
client.setRequestHeader("Connection", "close");
client.send("data=" + encodeURIComponent(data));    

data JSON. , API . , .

+3

Source: https://habr.com/ru/post/1784473/


All Articles