最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rest - javascript : how to send request in restful - Stack Overflow

programmeradmin1浏览0评论

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.setRequestHeader("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

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.setRequestHeader("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

Share Improve this question edited Jan 10, 2011 at 7:54 user495688 asked Jan 10, 2011 at 6:47 user495688user495688 9632 gold badges15 silver badges26 bronze badges 1
  • The same way you send requests normally, you just add more parameters. Please show us some more concrete code and a more concrete problem. – deceze Commented Jan 10, 2011 at 7:39
Add a ment  | 

1 Answer 1

Reset to default 4

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.

发布评论

评论列表(0)

  1. 暂无评论