JavaScript使用XMLHttpRequest 發送GET/Post 請求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<! DOCTYPE  HTML>
< html >
< head >
< title >Demo</ title >
< script  type="text/javascript">
     
      var xmlHttp; 
     
     //XmlHttpRequest对象
     function createXMLHttpRequest() { 
     var xmlHttp; 
     if (window.XMLHttpRequest) {  //非IE浏览器 
         xmlHttp = new XMLHttpRequest(); 
         if (xmlHttp.overrideMimeType) 
             xmlHttp.overrideMimeType('text/xml'); 
     } else if (window.ActiveXObject) {  //如果是IE浏览器
         try { 
             xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
         } catch (e) { 
             try { 
                 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
             } catch (e) { 
            
        
    
     return xmlHttp; 
   
    
     function getStatusBack(){ 
         if(xmlHttp.readyState == 4 && xmlHttp.status == 200){ 
             var b = xmlHttp.responseText; 
             alert(b);
             console.log(b);
         
        
    
    function testGet(){
     
      xmlHttp = createXMLHttpRequest(); 
      var url = "http://xxx/xxx/xxx" 
      xmlHttp.open("GET", url, true);// 异步处理返回  
      xmlHttp.onreadystatechange = getStatusBack;   //设置回调函数
      xmlHttp.setRequestHeader("Content-Type",  "application/x-www-form-urlencoded;"); 
      xmlHttp.send();  //发送请求
     }
     function testPost(){
         var parameter = "tidList=1"
          var url = "http://xxx/xxx/xxx"; 
        xmlHttp = createXMLHttpRequest();
        xmlHttp.open("POST", url, true); 
          xmlHttp.onreadystatechange = getStatusBack; //设置回调函数 
              xmlHttp.setRequestHeader("Content-Type", 
              "application/x-www-form-urlencoded;"); 
              xmlHttp.send(); //发送请求
     }
     
 
</ script >
</ head >
         
< body >
 
         < button   onclick="testGet()">Test Get</ button >
         < button   onclick="testPost()">Test Post</ button >
</ body >
</ html >

  


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/5952576.html,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Kotlin/JS中,可以使用JavaScript的原生XMLHttpRequest对象来发GETPOST。以下是一个简单的示例: 首先,需要在Gradle中添加以下依赖: ```groovy implementation 'org.jetbrains.kotlin:kotlin-stdlib-js' ``` 然后,可以编写一个包含发GETPOST的类: ```kotlin import org.w3c.xhr.XMLHttpRequest import kotlin.js.json class Http { companion object { fun get(url: String, callback: (String) -> Unit) { val xhr = XMLHttpRequest() xhr.open("GET", url) xhr.onload = { if (xhr.status == 200.toShort()) { callback(xhr.responseText) } else { // handle error } } xhr.send() } fun post(url: String, data: Map<String, String>, callback: (String) -> Unit) { val xhr = XMLHttpRequest() xhr.open("POST", url) xhr.setRequestHeader("Content-Type", "application/json") xhr.onload = { if (xhr.status == 200.toShort()) { callback(xhr.responseText) } else { // handle error } } xhr.send(JSON.stringify(data)) } } } ``` 这个类包含两个静态方法:`get`和`post`。这些方法接受一个URL和一个回调函数。在回调函数中,可以处理服务器响应。 要发GET,可以使用以下代码: ```kotlin Http.get("https://example.com/api/data", { response -> console.log(response) }) ``` 要发POST,可以使用以下代码: ```kotlin val data = mapOf("name" to "John", "age" to "30") Http.post("https://example.com/api/data", data, { response -> console.log(response) }) ``` 在这个示例中,POST了一个包含"name"和"age"字段的JSON对象。服务器可以使用这些值来处理请

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值