英文:
When calling sync fetch to call backend api, response only contain part of the data that is returned from backend
问题
I am working on a course project. We are using JavaScript for frontend. When calling async fetch, the response only contains part of the data returned by backend.
For example, if backend api returns:
{
"status": "success",
"content": [
{
name: "ABC",
key: "123456"
}
]
}
The response that frontend receives:
{"status": "success"}
Here is my code:
Frontend:
export async function getObjectsFromS3(data) {
const response = await fetch(url, {
method: "GET",
headers: {
"Authorization": `Basic ${btoa("user:password")}`
}
});
if (!response.ok) {
throw an Error(response.status);
}
const result = await response.json();
return result;
}
Backend:
@RestController
@RequestMapping("/users")
public class S3Controller {
@Autowired
S3Service s3Service;
@GetMapping("/{userName}/objects")
public String listObjects(@PathVariable String userName, @RequestParam String prefix) {
AuthenticationUtil.authorizeUser(userName);
String jsonString = "";
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
ja.put("key");
ja.put("name");
ja.put("size");
ja put("type");
try {
List<S3Object> objects = s3Service.listObjects(prefix);
for (S3Object object : objects) {
String suffix = object.key().replace(prefix, "");
String[] suffixDir = suffix.split("/");
if ((suffixDir.length == 1 && suffixDir[0] !== "")) {
String type, name, key;
key = object.key();
if (key.charAt(key.length() - 1) === '/')
type = "folder";
else
type = "file";
name = suffixDir[suffixDir.length - 1];
jsonString += object.key() + "," + name + "," + String.valueOf(object.size()) + "," + type + "\n";
}
}
jo.put("status", "success");
jo.put("content", CDL.toJSONArray(ja, jsonString));
} catch (Exception e) {
jo.put("status", "error");
jo.put("message", "Error when listing files: " + e.getMessage());
}
return jo.toString();
}
Here is the screenshot of DevTools' response. DevTool's Response
I have tried calling this API using Postman, and everything works. But when calling from the frontend, the response only contains part of the data.
英文:
I am working on a course project. We are using JavaScript for frontend. When calling async fetch, the response only contains part of the data returned by backend.
For example, if backend api returns:
{
"status": "success",
"content": [
{
name: "ABC",
key: "123456"
}
]
}
The reponse that frontend receives:
{"status": "success"}
Here is my code:
Frontend:
export async function getObjectsFromS3(data) {
const response = await fetch(url, {
method: "GET",
headers: {
"Authorization": `Basic ${btoa("user:password")}`
}
});
if (!response.ok) {
throw new Error(response.status);
}
const result = await response.json();
return result
}
Backend:
@RestController
@RequestMapping("/users")
public class S3Controller {
@Autowired
S3Service s3Service;
@GetMapping("/{userName}/objects")
public String listObjects(@PathVariable String userName, @RequestParam String prefix) {
AuthenticationUtil.authorizeUser(userName);
String jsonString = "";
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
ja.put("key");
ja.put("name");
ja.put("size");
ja.put("type");
try {
List<S3Object> objects = s3Service.listObjects(prefix);
for (S3Object object : objects) {
String suffix = object.key().replace(prefix, "");
String[] suffixDir = suffix.split("/");
if ((suffixDir.length == 1 && suffixDir[0] !== "")) {
String type, name, key;
key = object.key();
if (key.charAt(key.length() - 1) === '/')
type = "folder";
else
type = "file";
name = suffixDir[suffixDir.length - 1];
jsonString += object.key() + "," + name + "," + String.valueOf(object.size()) + "," + type + "\n";
}
}
jo.put("status", "success");
jo.put("content", CDL.toJSONArray(ja, jsonString));
} catch (Exception e) {
jo.put("status", "error");
jo.put("message", "Error when listing files: " + e.getMessage());
}
return jo.toString();
}
Here is the screenshot of DevTools' response.
DevTool's Response
I have tried calling this api using postman, and everything works. But when calling from frontend, the reponse only contains part of the data.
答案1
得分: 0
1 删除后端服务器中的所有逻辑,只返回一个简单的JSON对象。
2 将数据放在一个名为 payload
的对象中,格式如下:
"payload": {
"status": "success",
"content": [
{
"name": "ABC",
"key": 123456
}
]
}
英文:
Two things worth to try:
1 Remove all your logics in backend server, just return a simple JSON object.
2 Put your data in one payload
like this:
"payload": {
"status": "success",
"content": [
{
"name": "ABC",
"key": 123456
}
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论