使用Fetch API来发布XML [英] Using Fetch API to POST XML

查看:147
本文介绍了使用Fetch API来发布XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Fetch API来处理XML数据的POST,以避免XmlHttpRequest的跨域问题.我面临的问题是,尽管将Content-Type设置为"text/xml"(在这种情况下,这是唯一受支持的content-type标头),但我的请求的content-type被重置为text/plain,导致HTTP状态为415来自请求的内容.

I'm trying to use Fetch API to handle POST of XML data to avoid cross-origin issues with XmlHttpRequest. The issue I face is that despite setting my Content-Type to 'text/xml' (which is the only supported content-type header in this case) my request's content-type is being reset to text/plain resulting in a HTTP status of 415 from the requested content.

这是我的提取函数:

    function doFetch(Content)
    {
        return fetch(
        URL, { method: 'POST',
           mode: 'no-cors',
           headers: new Headers(
           {'Content-Type': 'text/xml; charset=utf-8',
            'Accept': '*/*',
            'Accept-Language': 'en-GB',
            'Accept-Encoding': 'gzip, deflate',
            'Connection': 'Keep-alive',
            'Content-Length': Content.length                
            }),
            body: Content
           });
    }

内容是一串XML数据.

Content is a string of XML data.

这是实际使用的标头:

    Content-Length:1537
    content-type:text/plain;charset=UTF-8

只是想知道我尝试做的事情是否可能,而这正是我做错的事情吗? (我是网络开发的新手.)

Just wondering if what I'm trying to do is possible, and if it is what I'm doing wrong? (I'm kind of new to web-development).

谢谢!

推荐答案

由于您为请求设置了mode: 'no-cors'(为什么?…),浏览器不允许您设置除 CORS安全列出的请求标头.请参见规范要求:

Because you’re setting mode: 'no-cors' (why?…) for the request, browsers won’t allow you to set any request headers other than CORS-safelisted request-headers. See the spec requirements:

…如果警卫是"request-no-cors"并且名称/不是

…if guard is "request-no-cors" and name/value is not a CORS-safelisted request-header, return.

设置Content-Type: text/xml; charset=utf-8使其不再是CORS安全列出的请求标头;如果Content-Type的值为application/x-www-form-urlencodedmultipart/form-datatext/plain,则它只是CORS安全列出的请求标头.

Setting Content-Type: text/xml; charset=utf-8 makes it no longer a CORS-safelisted request-header; Content-Type is only a CORS-safelisted request-header if its value is application/x-www-form-urlencoded, multipart/form-data, or text/plain.

所以这里解决方案的开始是不使用mode: 'no-cors'.

So the start of a solution here is to not use mode: 'no-cors'.

通常只有在设置mode: 'no-cors'时才有意义,如果您使用Service Workers来缓存响应,则由于mode: 'no-cors'请求,浏览器将不允许您的脚本访问响应的任何属性,因此,您唯一可以使用的功能就是对其进行缓存.

The only case where it usually makes sense to set mode: 'no-cors' is if you’re using Service Workers for caching responses—because for a mode: 'no-cors' request, browsers won’t let your script access any properties of the response, so the only useful thing you can do with it is cache it.

我正在尝试使用Fetch API处理POST的XML数据,以避免XmlHttpRequest的跨域问题.

I'm trying to use Fetch API to handle POST of XML data to avoid cross-origin issues with XmlHttpRequest.

Fetch API遵循与XHR相同的跨域请求模型,因此目前尚不清楚您希望通过使用Fetch API避免哪些跨域问题……

The Fetch API follows the same cross-origin request model as XHR, so it’s unclear what cross-origin issues you’d be attempting to avoid by using the Fetch API…

这篇关于使用Fetch API来发布XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆