为什么我的服务器没有收到任何内容?
Why isn't my server receiving any content?
我有这个:
function base64ToBlob(base64, mime)
{
mime = mime || '';
var sliceSize = 1024;
var byteChars = window.atob(base64);
var byteArrays = [];
for (var offset = 0, len = byteChars.length; offset < len; offset += sliceSize) {
var slice = byteChars.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
return new Blob(byteArrays, {type: mime});
}
function send() {
let blob = base64ToBlob(picture, "image/jpeg");
let formData = new FormData();
formData.append("picture", blob)
formData.append("a", 70)
formData.append("b", 5)
var request = new XMLHttpRequest();
request.open("GET", "server/far/away/that/need/content-type");
request.setRequestHeader("Content-type", "HELP HERE")
request.send(formData);
}
当我调用发送函数时,我的服务器(firebase 的云函数)没有
接收任何数据。我认为那是因为没有内容类型,服务器无法使用我发送的数据。
这就是我需要内容类型的原因。
抱歉,我的英语不完美:/(我是法国人)
When I call the send function, my server (cloud function for firebase) don't receive any data.
那是因为您使用了错误的 HTTP 方法。 GET
没有正文,但它是您发送数据的正文:
var request = new XMLHttpRequest();
request.open("GET", "server/far/away/that/need/content-type");
// ^^^^----------------------------------------------- method
request.setRequestHeader("Content-type", "HELP HERE")
request.send(formData);
// ^^^^^^^^------------------------------------------- request body
来自 MDN:
send()
accepts an optional parameter which lets you specify the request's body; this is primarily used for requests such as PUT
. If the request method is GET
or HEAD
, the body parameter is ignored and the request body is set to null.
内容类型可能无关紧要,但如果您像那样在正文中发送数据,则需要使用 POST
或 PUT
。
我有这个:
function base64ToBlob(base64, mime)
{
mime = mime || '';
var sliceSize = 1024;
var byteChars = window.atob(base64);
var byteArrays = [];
for (var offset = 0, len = byteChars.length; offset < len; offset += sliceSize) {
var slice = byteChars.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
return new Blob(byteArrays, {type: mime});
}
function send() {
let blob = base64ToBlob(picture, "image/jpeg");
let formData = new FormData();
formData.append("picture", blob)
formData.append("a", 70)
formData.append("b", 5)
var request = new XMLHttpRequest();
request.open("GET", "server/far/away/that/need/content-type");
request.setRequestHeader("Content-type", "HELP HERE")
request.send(formData);
}
当我调用发送函数时,我的服务器(firebase 的云函数)没有 接收任何数据。我认为那是因为没有内容类型,服务器无法使用我发送的数据。
这就是我需要内容类型的原因。
抱歉,我的英语不完美:/(我是法国人)
When I call the send function, my server (cloud function for firebase) don't receive any data.
那是因为您使用了错误的 HTTP 方法。 GET
没有正文,但它是您发送数据的正文:
var request = new XMLHttpRequest();
request.open("GET", "server/far/away/that/need/content-type");
// ^^^^----------------------------------------------- method
request.setRequestHeader("Content-type", "HELP HERE")
request.send(formData);
// ^^^^^^^^------------------------------------------- request body
来自 MDN:
send()
accepts an optional parameter which lets you specify the request's body; this is primarily used for requests such asPUT
. If the request method isGET
orHEAD
, the body parameter is ignored and the request body is set to null.
内容类型可能无关紧要,但如果您像那样在正文中发送数据,则需要使用 POST
或 PUT
。