gpt4 book ai didi

javascript - 发送此 AJAX 发布数据的正确内容类型

转载 作者:行者123 更新时间:2023-11-29 20:56:00 25 4
gpt4 key购买 nike

我在使用 ajax post 发送 base64 图像数据时遇到问题我想我的 Content-Type 值有误,但我尝试了 application/jsontext/jsonimage/jpeg 但都没有成功

Javascript

  function sendFormData(fD)
{
var urls = fD.get('urls');
console.log('urls', urls);

var xhr = new XMLHttpRequest();
xhr.open('POST', '/editsongs.update_artwork');
alert(urls);
xhr.setRequestHeader("Content-type", "image/jpeg");
xhr.send(urls);
}

浏览器控制台显示

["data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMTEhUTExMWFhUXGRgbGBgXGR0aGRgXHRgYFx4YGxkYHiogGh4lGxgdIjEhJSkrLi4uGh8zODMtNygtLisBCgoKDg0OGhAQGy0lHSUtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLf/AABEIANEA8QMBIgACEQEDEQH/xAAcAAACAgMBAQAAAAAAAAAAAAAEBQMGAAECBwj/xABGEAACAQIEBAQDBAUJCAIDAAABAhEDIQAEEjEFIkFREzJhcQaBkRRCobEjUsHh8AcVM2JygsLR8RZDRFNzg5KyJDSzw8T/xAAZAQADAQEBAAAAAAAAAAAAAAABAgMABAX/xAAqEQACAgEDBQACAQQDAAAAAAAAAQIRAxIhMQQTFEFRImGRYnGh8AUjMv/aAAwDAQACEQMRAD8AsoosJt9cSpSdeYKYEXxKlQs4E2kEx6dMWJFBEDr0OPey5nCrR58MalwJUpB1nENbJWthpTyQDG5H5Y0gGqNQOJLLXA7h9K9VypGITSPbD+tSGIFy4mSJx0xz7EXjE7UscacNamW30jAlbLsNxiscqZNwoCK4wDEzJjkLiuoQjGNEYnCY5ZcawkMY5bEkY5GGsUijG1XtjcYPyFAQWMbbdsac9Ks0Y2wQphlwTKLUZg2wAwFmq8m5HYY5HFTRph0VnBI1QDZZgsZGwEnHPmn/ANb+lMcVqLFWRafKMTU+L6bRb88V/LcY+0otQKVmbdcbq1wsSYkgDfc7C3fHIoRlDVIs5tSpFzyXEtcdCdvlhopwp4RSpwvcAf64bj0x5mbSpUjrhdbmicSU1xi08djEWylGRjRGOsC5uodl379sBK2F8HbMAJxUON5lqrQFMA4f1CVU62ke2FaZlWqXgAbepx29MtD1VZzZXaol4RwYKsuAWP4…

Java 服务器代码

public String updateArtwork(Request request, Response response)
{
System.out.println("Received artwork");

for(String s:request.queryParams())
{
System.out.println("---"+s);
}
System.out.println("ReadParms");
return "";
}

只是输出

Received artwork
ReadParms

已更新为改为以表格形式发送

// Once we got everything, time to retrieve our objects
function sendData()
{
var fD = new FormData();

// send Files data directly
var files = imgList.filter(
function isFile(obj)
{
return obj.type === 'file';
}
);

files.forEach(
function appendToFD(obj)
{
fD.append('files[]', obj.file);
}
);

// for elems, we will need to grab the data from the server
var elems = imgList.filter(
function isElem(obj)
{
return obj.type === "element";
}
);

var urls = elems.map(
function grabURL(obj)
{
return obj.element.src;
}
);

if (urls.length)
fD.append('urls', JSON.stringify(urls));

sendFormData(fD);
};

function sendFormData(fD)
{
// but here we will just log the formData's content
var files = fD.getAll('files[]');
console.log('files: ', files);
var urls = fD.get('urls');
console.log('urls', urls);

var xhr = new XMLHttpRequest();
xhr.open('POST', '/editsongs.update_artwork');
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(fD);
}

然后在我的服务器上

 public String updateArtwork(Request request, Response response)
{
System.out.println("Received artwork");

for(String s:request.queryParams())
{
System.out.println("***"+s);
System.out.println(request.queryParams(s));
}
System.out.println("ReadParms");
return "";
}

及其输出

    Received artwork
***-----------------------------330219842643
Content-Disposition: form-data; name
"urls"

["data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMSEhUSExIWFhUXFxgXGBcYFRgXFxkdGBcWGBgYFx0YHSggHR0lHRkYITEhJSkrLi4uFyA1ODMtNygtLisBCgoKDg0OFQ8PFSsZFRkrLSstLSstKysrLS03KystLSstKy03LSstLSstNzc3KysrLS0tKysrKysrKysrKysrK//AABEIAKoBKQMBIgACEQEDEQH...."]
-----------------------------330219842643--

ReadParms

所以我现在正在获取数据,但我并不真正理解如何解析 Java 中的 Content-Disposition 部分。

这段代码最初不是我写的,正如您所看到的 FormData 是构造的,它不是来自实际的表单。我的第一次尝试是尝试从 FormData 中提取并以不同的方式发送,另一种方法是首先不存储在 FormData 中,但不知道如何执行此操作.

更新 2尝试只发送第一个 url 而不是 formdata 或 arrya of urls,因为实际上只有一个 url。但它不起作用,服务器没有收到任何东西?

function sendFormData(urls)
{
console.log('urls', urls[0]);
var xhr = new XMLHttpRequest();
xhr.open('POST', '/editsongs.update_artwork');
xhr.setRequestHeader("Content-type", "text/json");
alert(JSON.stringify(urls[0]));
xhr.send(JSON.stringify(urls[0]));
}

最佳答案

您正在尝试使用 queryParams() 查看正文中的数据,这将为您提供位于 url 中的查询参数。

使用 body() 从请求主体加载数据。

关于javascript - 发送此 AJAX 发布数据的正确内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49325607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com