我想伪造一个服务器,其响应采用 xml 格式。我怎样才能做到这一点?
server.respondWith(
"GET",
"testurl",
[
200,
{
"Content-Type": "application/xml",
"Access-Control-Allow-Origin": "*",
"Cache-Control": "max-age=0, must-revalidate",
"Connection": "close"
},
<note></note>
]
);
最佳答案
当您使用 respondWith
模拟回复时,该方法的第三个参数是一个描述所需响应的数组。数组的第三个元素,其中 <note></note>
是响应的正文,因此您可以将 XML 作为字符串放置在那里。
var xml = getXmlStringSomehow();
server.respondWith(
"GET",
"testurl",
[
200,
{
"Content-Type": "application/xml",
"Access-Control-Allow-Origin": "*",
"Cache-Control": "max-age=0, must-revalidate",
"Connection": "close"
},
xml
]);
关于javascript - sinon fakeserver可以发送xml响应吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35305673/