简体   繁体   English

XmlHttpRequest POST方法在IE11中失败,状态码为400

[英]XmlHttpRequest POST method fails with status code 400 in IE11

I have a React app containing a JavaScript method which posts data to a server method. 我有一个React应用程序,其中包含将数据发布到服务器方法的JavaScript方法。 This method works fine in every browser under the sun...except IE11 (shocking I know). 这种方法在所有浏览器下都能正常工作……除了IE11(我知道令人震惊)。 Unfortunately IE11 support is a requirement for this project. 不幸的是,此项目需要IE11支持。

IIS logs reveal 400 (bad request) HTTP status codes. IIS日志显示400(错误请求)HTTP状态代码。

I chose not to use fetch() due to its incompatibility with IE11, and I'd rather avoid resorting to external libraries like axios or jQuery for a single method for a single browser. 我选择不使用fetch(),因为它与IE11不兼容,而我宁愿避免对单个浏览器使用单个方法求助于axios或jQuery之类的外部库。

I attempted a few Content-Type header values (application/json and x-url-form-encoded), and also tried various other headers which may or may not be related (Pragma, CORS - even though it's not cross-origin, Cache-Control et al.). 我尝试了一些Content-Type标头值(application / json和x-url-form-encoded),还尝试了可能相关或不相关的各种其他标头(Pragma,CORS-即使它不是跨源的,Cache) -Control等)。

handleSubmit(event) {
    const booking = {
        'key1': 'value1',
        'key2': 'value2',
        'key3': 'value3',
    };

    const xhr = new XMLHttpRequest();
    const url = "api/Booking/AddBooking";

    xhr.open("POST", url);
    xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    xhr.send(JSON.stringify(booking));

    xhr.onreadystatechange = () => {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var response = JSON.parse(xhr.responseText);

            this.setState({
                bookingResponse: response,
                showModal: true
            });
        }
    }
    event.preventDefault();
}

I know the external libraries work with IE11 so there must be a way around this with vanilla JavaScript, I just can't find what it is. 我知道外部库可与IE11一起使用,因此必须使用香草JavaScript来解决此问题,我只是找不到它。 Any ideas? 有任何想法吗?

The issue was related to some of the variable values. 问题与某些变量值有关。 I was using ES6 string interpolation (ie back ticks), which is unsupported in IE. 我使用的是ES6字符串插值(即反向滴答),而IE中不支持。

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

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