IE:使用AJAX发送POST数据

时间:2018-04-25 04:38:08

标签: javascript ajax internet-explorer

我正在使用POST发送AJAX个数据:

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进行调试时,服务器每次都会收到数据。难道没有人向我解释这种行为,以及如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

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

e.g:

    $.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.
      }
    });