skip to Main Content

template.html

<input type="text" id="text">
<input id="submit" name="submit"  type="submit">
<div id="roro"></div>

<script type="text/javascript">
$('#submit').click(function(){
    $.ajax({
        url:'',
        data : {'text':$('#submit').val()},
        type: 'POST',
        dataType : 'json',
        success:function(data){
            $('#roro').text(result['data']);
        },
        error:function(r){
            alert(text);
        }

   });
});
</script>

view.py

@csrf_exempt
def construction(request):
    if request.method == 'POST':
        text = request.POST['text']
        return render(request, 'pybo/construction_compare.html', text)
    return render(request, 'pybo/construction_compare.html')

It’s a test for pass of variables.
So if it’s failed I want see variable on alert..

I found many of articles but I failed..
And It has many fault because I studying alone and My brain is not good 🙁

2

Answers


  1. you have to specify the url in your ajax request.

    Login or Signup to reply.
  2. You should specify the url to the view function to the url in your script.

    <script type="text/javascript">
        $('#submit').click(function(){
        $.ajax({
            url:"{% url 'the_url_to_view' %}",
            data : {'text':$('#submit').val()},
            type: 'POST',
            dataType : 'json',
            success:function(data){
            $('#roro').text(result['data']);
        },
        error:function(r){
            alert(text);
        }
    
      });
    });
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search