0
   <script>

        $(document).ready(function () {
            $(window).on('beforeunload', function () {
                $.ajax({
                    // type: 'GET',
                    url: "{% url 'size_reducer:data_delete' id %}",
                    dataType: 'json',
                    success: function (data) {
                        console.log('ok');

                    }
                })
            });
        });
    </script>

I want to pass id in ajax URL but it is giving me an error because it's not getting id

2 Answers 2

2

you cannot use django url tag in ajax like this. your url should be something like

url: "/<path_to_data_delete>/" + id 

So first save the id in javascript variable and append the variable to url string as per your declared url.

Remember not to use the domain name before /<path_to_data_delete>. If you are running site on local server, skip the domain name. i.e http://127.0.0.1:8000.

Sign up to request clarification or add additional context in comments.

1 Comment

suppose id is my javascript variable, how can I access the javascript variable inside the ajax
2

You can use Template literals to pass Javascript variable

<script>
$.ajax({
  // Can use django url if using <script> tag inside template
  url: `{% url 'size_reducer:data_delete' ${id} %}`, 
  dataType: 'json',
  success: function (data) {
     console.log('ok');
  }
})
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.