javascript : how to send request in restful
Hello I want to ask you something how do I send a request to the webservice (restful) if the parameters which I will send more than one parameter?
edit:
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.setRequestHe开发者_Go百科ader("Content-Type", "text/plain");
client.send(oriText,stemText,folText);
if (client.status == 200){
client.responseText;
}
else{
client.statusText;
}
}
client.send --> contents parameters which i want send to server
If you are making a request for data, you should use a GET request. Any parameters required to fetch the correct data should 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 a 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));
Typically data
would be a JSON string. Of course, all of this depends on the API of the service. Without knowing these details, I cannot help beyond the typical example above.
精彩评论