Javascript 无法在Safari中使用诸如text()、json()、arrayBuffer()等请求函数
使用以下示例在Safari控制台中创建新的POST请求时Javascript 无法在Safari中使用诸如text()、json()、arrayBuffer()等请求函数,javascript,safari,Javascript,Safari,使用以下示例在Safari控制台中创建新的POST请求时 var request = new Request('/', { method: 'POST', body: '{ "foo": "1", "bar": 2 }' }); request.json().then(function(json) { console.log(json.foo); console.log(json.bar); }); 但是,在我的应用程序中,当我尝试访问request.text()时,有一
var request = new Request('/', {
method: 'POST',
body: '{ "foo": "1", "bar": 2 }'
});
request.json().then(function(json) {
console.log(json.foo);
console.log(json.bar);
});
但是,在我的应用程序中,当我尝试访问request.text()时,有一个错误状态为“NotSupportedError:该操作不受支持”,我不确定是什么原因导致了这种情况
function sendRequest(input,requestInit){
// create request object for POST request
var request = new Request(input.url, requestInit);
// trying to read the clone body will throw the above error
Var requestclone = request.clone().text().then(text=>console.log(text)).catch(err=>console.log(err));
fetch(request).then(response=>{
//do stuff here
});
当使用GET或PUT请求运行同一个函数时,.text()函数可以工作,我不会遇到任何问题。“不支持”错误仅在POST请求时发生
如有任何见解,将不胜感激
编辑1:
当body键是formData对象时,在Safari和Chrome中运行以下示例会产生不同的结果
var formDataObj = new FormData();
formDataObj.append('key','value')
var requestOptions = {
cache: "default",
credentials: "same-origin",
method: "POST",
mode: "cors"
}
var requestOptionsString = {
body: "text",
cache: "default",
credentials: "same-origin",
method: "POST",
mode: "cors"
}
var requestOptionsJSON = {
body: {"key":"value"},
cache: "default",
credentials: "same-origin",
method: "POST",
mode: "cors"
}
var requestOptionsFD = {
body: formDataObj,
cache: "default",
credentials: "same-origin",
method: "POST",
mode: "cors"
}
function requestTest(input,options){
var request = new Request(input,options)
request.clone().formData().then(formData=>console.log("formData",input,formData)).catch(err=>console.log("noFormData",input,err))
request.clone().json().then(json=>console.log("json",input,JSON.stringify(json))).catch(err=>console.log("noJson",input,err))
request.clone().text().then(text=>console.log("text",input,text)).catch(err=>console.log("noText",input,err))
request.clone().arrayBuffer().then(arrayBuffer=>console.log("arrayBuffer",input,arrayBuffer)).catch(err=>console.log("noArrayBuffer",input,err))
request.clone().blob().then(blob=>console.log("blob",input,blob)).catch(err=>console.log("noBlob",input,err))
fetch(request).then(response=>console.log("response",input,response)).catch(error=>console.log("error",input,error))
}
requestTest('none',requestOptions)
requestTest('string',requestOptionsString)
requestTest('json',requestOptionsJSON)
requestTest('formData',requestOptionsFD)
有趣的是,它可以在Chrome、Firefox和new Edge等Chrome浏览器中使用。如果它适用于其他请求类型,则可能是一个bug。是的,我注意到这个问题的唯一原因是因为我通常通过Puppeter通过Chrome运行测试,所以我从未想到在safari中运行时会出现问题。有趣的是,它适用于Chrome、Firefox和new Edge等Chrome浏览器。如果它适用于其他请求类型,则可能是一个bug。是的,我注意到这个问题的唯一原因是因为我通常通过Puppeter通过Chrome运行测试,所以我从未想到在safari中运行时会出现问题。