Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

개발 공부

클론코딩 4- Ajax 본문

카테고리 없음

클론코딩 4- Ajax

방구석개발입문 2022. 12. 4. 14:42
<!-- jquery 사용하기 위해 -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div id="basket"; style = "background: ghostwhite; width: 145px; height: 120px; position: fixed; right: 85% ;text-align : center; line-height : 50px; cursor:pointer">
    <p id="menu1_menuName" style="font-size:120%">{{ menu1.menuName }}</p>
    <p id= "menu1_price" style="font-size:155%">{{ menu1.price }}</p>

 

<!-- Optional JavaScript; choose one of the two! -->
<script >

    document.getElementById("basket").onclick = function () {

        var basket={
            menuName : document.getElementById('menu1_menuName').innerText
            ,price : document.getElementById('menu1_price').innerText
        }

        $.ajax({
            url: '/menu/upload',
            dataType: 'json',
            data: basket,
            type: 'post',

        })
    };


</script>

 

 

 

view

class upload(APIView):
    def post(self, request):
        menuName = request.data.get("menuName")
        price = request.data.get("price")

        print(menuName)
        print(price)

        basket.objects.create(menuName=menuName, price=price)

        return Response(status=200)

 

 

url

path('menu/upload',upload.as_view()),