【问题标题】:IE: Sending POST data with AJAXIE:使用 AJAX 发送 POST 数据
【发布时间】:2018-04-25 14:52:21
【问题描述】:

我用AJAX发送POST数据:

const form = d.getElementById('form');
form.addEventListener('submit', SendData);

function SendData(e) {
    e.preventDefault();
    var data = e.target.getElementsByTagName('input')[0].value.trim();

    var xhr = new XMLHttpRequest();
    xhr.addEventListener('load', function(event){
        console.log(event.target.responseText);
    });

    xhr.addEventListener('error', function(event){
        console.log(event.target.statusText);
    });
    xhr.open('POST', '/db', true);
    xhr.send('data=' + data);
}

但是当我使用IE11时,服务器每两个请求只接收一次数据:

1:

POST http://localhost:99/db HTTP/1.1
Accept: */*
Referer: http://localhost:99/
Accept-Language: ru
Content-Type: text/plain;charset=UTF-8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like 
Gecko
Host: localhost:99
Content-Length: 13
Connection: Keep-Alive
Cache-Control: no-cache

data=01234567

2:

POST http://localhost:99/db HTTP/1.1
Accept: */*
Referer: http://localhost:99/
Accept-Language: ru
Content-Type: text/plain;charset=UTF-8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like 
Gecko
Host: localhost:99
Content-Length: 13
Connection: Keep-Alive
Cache-Control: no-cache

我注意到,当我使用Fiddler 进行调试时,服务器每次都会收到数据。没有人能向我解释这种行为,以及如何解决它吗?

【问题讨论】:

  • 同样的问题? link

标签: javascript ajax internet-explorer


【解决方案1】:

您是否尝试过使用 jQuery ajax 功能?这样做的好处是它可以完美地跨浏览器工作,并且只需要一种语法。我知道,大多数人不喜欢使用额外的框架,但这个框架大大简化了编码。

例如:

    $.ajax({
      url:'https://my.server.com/myscript.php,
      type:'post',
      data: {
         var1: "x",
         var2: "y",
      },
      success: function(data) {
         // here goes the data, returned from your php script e.g.
      }
    });

【讨论】:

  • jquery同样的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-01
  • 2011-02-21
  • 1970-01-01
  • 1970-01-01
  • 2011-05-22
  • 2014-11-20
  • 1970-01-01
相关资源
最近更新 更多