简体   繁体   English

当我尝试在 django mvt 中使用 $.ajax 时,它无法正常工作,甚至没有显示错误

[英]When I am trying to use $.ajax in django mvt its not working not even showing error

This is the view section这是视图部分

class ApproveExpense(View):
    def post(self,request,id):
        csrf_token = get_token(request)
        select_expense= get_object_or_404(ExpensePortal,id=id)
        status_value = request.POST.get("a")
        print(status_value)
        if status_value == 'Accept':
            select_expense.status = True
            select_expense.save()
        else:
            pass

        return JsonResponse({'message':"Expense Approved"}, status=200)

This is the template code这是模板代码

{% extends "base.html" %}
{% load static %}
{% block body %}


<table>
    <tr>
        <th>Date</th>
        <th>Description</th>
        <th>Amount</th>
        <th>Remarks</th>
        <th>Bill</th>
        <th>Download</th>
        <th>Status</th>
    </tr>

    {% for i in detail %}
    <tr>
        <td>{{ i.date }}</td>
        <td>{{ i.description }}</td>
        <td>{{ i.amount }}</td>
        <td>{{ i.remarks }}</td>
        <td><img src = "{{ MEDIA_URL }}{{i.bill.url}}" alt="Expense-bill" width="50" height="50"></td>
        <td><a href= "{% url 'users:download1' i.bill %}"><img src="{% static 'assets/img/flat.png' %}"height="33"width="50"></a></td>
        {% if current_user.user_type == 'ADMIN' or current_user.user_type == 'ACCOUNTS'  %}
        <td><button id="st" value="Accept" data-catid="{{ i.id }}">App</button>&nbsp;<button id="st1" name="b" onclick="Reject()" value="Reject" data-catid="{{ i.id }}">Reject</button></td>
        {% elif i.status == True %}
        <td style="color:green;">Approved</td>
        {% elif i.status == False %}
        <td style="color:red;">Rejected</td>
        {% else %}
        <td style="color:purple;">Pending</td>
        {% endif %}


    </tr>

    {% endfor %}
        <tr style="margin-top:60px">
           <td colspan ="2"><h3>Total</h3></td><td colspan="4" style="text-align:left;padding:40px"><h4>{{ total }}</h4></td>
       </tr>
   </table>

</table>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var catid = document.getElementById('st1').getAttribute("data-catid")
var csrf = document.cookie.match("csrftoken").input.split('=')[1]
var current_value = document.getElementById('st1').value
      $("#st").click(function() {


        $.ajax(
        {
        url:  '/approve/'+catid,
        headers:{'X-CSRFToken':csrf},
        type:  'POST',
        data:{'a',current_value},
        success: function(data) {
        console.log(data)

        }
        })
      });
});

</script>

Finally i found the solution i switched to XMLHttpRequest here is the modification for views and templates最后我找到了我切换到 XMLHttpRequest 的解决方案这里是视图和模板的修改

This is views code这是查看代码

class ApproveExpense(View):
    def post(self,request,id):
        csrf_token = get_token(request)
        select_expense= get_object_or_404(ExpensePortal,id=id)
        status_value = json.loads(request.body)
        print(status_value["current_value"])
        if status_value["current_value"] == 'Accept':
            select_expense.status = True
            select_expense.save()
        else:
            pass

        return JsonResponse({'message':"Expense Approved"}, status=200)

This is templates code这是模板代码

<table>
    <tr>
        <th>Date</th>
        <th>Description</th>
        <th>Amount</th>
        <th>Remarks</th>
        <th>Bill</th>
        <th>Download</th>
        <th>Status</th>
    </tr>

    {% for i in detail %}
    <tr>
        <td>{{ i.date }}</td>
        <td>{{ i.description }}</td>
        <td>{{ i.amount }}</td>
        <td>{{ i.remarks }}</td>
        <td><img src = "{{ MEDIA_URL }}{{i.bill.url}}" alt="Expense-bill" width="50" height="50"></td>
        <td><a href= "{% url 'users:download1' i.bill %}"><img src="{% static 'assets/img/flat.png' %}"height="33"width="50"></a></td>
        {% if current_user.user_type == 'ADMIN' or current_user.user_type == 'ACCOUNTS'  %}
        <td><button onclick="approve()" id="st" value="Accept" data-catid="{{ i.id }}">App</button>&nbsp;<button id="st1" onclick="reject()" value="Reject" data-catid="{{ i.id }}">Reject</button></td>
        {% elif i.status == True %}
        <td style="color:green;">Approved</td>
        {% elif i.status == False %}
        <td style="color:red;">Rejected</td>
        {% else %}
        <td style="color:purple;">Pending</td>
        {% endif %}


    </tr>

    {% endfor %}
        <tr style="margin-top:60px">
           <td colspan ="2"><h3>Total</h3></td><td colspan="4" style="text-align:left;padding:40px"><h4>{{ total }}</h4></td>
       </tr>
   </table>

</table>
</script>
<script>
function approve(){
var catid = document.getElementById('st').getAttribute("data-catid")
var csrf = document.cookie.match("csrftoken").input.split('=')[1]
var current_value = document.getElementById('st').value
var xhr = new XMLHttpRequest();
var url = '/approve/'+catid+'/'
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("X-CSRFToken", csrf);
xhr.onreadystatechange = function() {
                    if (xhr.readyState === 4 && xhr.status === 200) {


                    }
                };
                     var data = JSON.stringify({ current_value

                });

                xhr.send(data);
                }
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用Ajax访问页面时,为什么会出现Access-Control-Allow-Origin错误? - Why am I getting Access-Control-Allow-Origin error when trying to use ajax to access a page? 我正在尝试在 JS 中使用可选链接,但它显示错误 - I am trying to use optional chaining in JS and it showing error 尝试使用其Ajax功能时Spine.js错误 - Spine.js error when trying to use its Ajax features 当我试图运行mupx部署时,它显示错误 - when i am trying to run mupx deploy it is showing error 我正在尝试选中其主题复选框的tbody的所有复选框,但无法正常工作 - I am trying to tick all the checkboxes of tbody when checkbox of its thead is checked but its not working 我正在尝试返回 function 但显示错误 - I am trying to return function but showing error 当我尝试在导航中使用道具时出现错误 - Getting Error when i am trying to use props in Navigation 当我尝试使用锚点和ID滚动页面时,它不能正常工作 - when i am Trying to scroll the page using anchor and id, its not working fine 我正在尝试在程序中使用猫鼬模式,但它一直显示错误用户不是构造函数 - I am trying to use mongoose Schema in my program but it keeps on showing error User is not a constructor 我正在尝试在我的 react redux 应用程序中使用 map。 但使用useDispatch后,控制台显示错误 - I am trying to use map in my react redux app. but after using useDispatch, the console is showing error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM