简体   繁体   English

IE:使用AJAX发送POST数据

[英]IE: Sending POST data with AJAX

I'm sending POST data with AJAX : 我正在使用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);
}

But when I use IE11 , server receives data only once in every two requests: 但是,当我使用IE11 ,服务器每两个请求仅接收一次数据:

1: 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: 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

I noticed, when I use Fiddler for debugging, server receives data every time. 我注意到,当我使用Fiddler进行调试时,服务器每次都会接收数据。 Couldn't anybody explain to me this behavior, and how to fix it? 没人能向我解释这种行为,以及如何解决它吗?

Did you try to use jQuery ajax functionality? 您是否尝试过使用jQuery ajax功能? This has the advantage that it is working across browsers flawlessly, and needs only one syntax. 这样做的好处是,它可以完美地跨浏览器运行,并且只需要一种语法。 I know, that most people do not like to use additional frameworks, but this one simplifies coding a lot. 我知道,大多数人不喜欢使用其他框架,但是这种框架大大简化了编码。

eg: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM