강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

이상민님의 프로필 이미지
이상민

작성한 질문수

비전공자를 위한 진짜 입문 올인원 개발 부트캠프

[그랩마켓] axios로 네트워크 통신하기

products가 출력되지 않습니다 !

작성

·

179

1

안녕하세요 먼저 좋은 강의 항상 감사드립니다.

다름이 아니라 products 이미지가 출력되지 않아서요 !

오류창 입니다.

Failed to load resource: the server responded with a status of 404 ()

index.html:102 error 발생 :  Error: Request failed with status code 404

    at e.exports (isAxiosError.js:10:52)

    at e.exports (isAxiosError.js:10:52)

    at XMLHttpRequest.E (isAxiosError.js:10:52)

index.html 코드입니다.

<html>
<head>
<title>그랩마켓</title>
<link href="index.css" type="text/css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="header">
<div id="header-area">
<img src="images/icons/logo.png" />
</div>
</div>
<div id="body">
<div id="banner">
<img src="images/banners/banner1.png" />
</div>
<h1>판매되는 상품들</h1>
<div id="product-list"></div>
</div>
<div id="footer"></div>
</body>
<script>
axios
.get("https://1a135b44-e524-4168-8a97-42364bfed983.mock.pstmn.io")
.then(function (result) {
console.log("통신결과 : ", result);

const products = result.data.products;

let productsHtml = "";

for (var i = 0; i < products.length; i++) {
let product = products[i];

productsHtml =
productsHtml +
'<div class="product-card">' +
"<div>" +
'<img class="product-img" src="' +
product.imageUrl +
'" alt="" />' +
"</div>" +
'<div class="product-contents">' +
'<span class="product-name">' +
product.name +
"</span>" +
'<span class="product-price">' +
product.price +
"" +
"</span>" +
'<div class="product-seller">' +
'<img class="product-avatar" src="images/icons/avatar.png" alt="" />' +
"<span>" +
product.seller +
"</span>" +
"</div>" +
"</div>" +
"</div>";
}

document.querySelector("#product-list").innerHTML = productsHtml;
})

.catch(function (error) {
console.error("error 발생 : ", error);
});
</script>
</html>
아래는 포스트맨 코드입니다.
{

"products" : [

{

"name": "농구공",

"price": 10000,

"seller": "조던",

"imageUrl": "images/products/basketball1.jpeg"

},

{

"name": "축구공",

"price": 50000,

"seller": "메시",

"imageUrl": "images/products/soccerball1.jpg"

},

{

"name": "키보드",

"price": 10000,

"seller": "그랩",

"imageUrl": "images/products/keyboard1.jpg"

}

]

}
어떤게 문제인지 알 수 있을까요?
 

답변 1

0

그랩님의 프로필 이미지
그랩
지식공유자

현재 에러는 네트워크 응답이 제대로 오지 않아 생기는 이슈입니다 (404)

axios.get 요청을 하는 URL을 보면 /products가 빠져있어서 요청이 제대로 가지 않는 것 같아요!

아래와 같이 바꿔보시면 정상적으로 동작하실 거예요.

https://1a135b44-e524-4168-8a97-42364bfed983.mock.pstmn.io/products

이상민님의 프로필 이미지
이상민

작성한 질문수

질문하기