0

When i click on a href they go on Script code but form Script not call the views.py function!

Link code::

<a class="likebutton" href="#">
    <span id="unlock">
        <i class="fas fa-lock"></i>&nbsp;Send Message
    </span>
</a>

Script code::

<script type="text/javascript">
  $('.likebutton').click(function(){ 
    $.ajax( 
    { 
        type:"GET", 
        url: "sendnotification",  
        success: function(){  
          alert('Done')
        } }) });
</script>

urls.py::

url(r"^sendnotification",views.sendnotification,name='sendnotification'),

views.py::

@csrf_exempt
def sendnotification(request):
  if request.method == 'GET':
      push_service = FCMNotification(api_key="*********")
      registration_id = "****************"
      data_message = {
      "Nick" : "Mario",
      "body" : "great match!",
      "Room" : "PortugalVSDenmark"}
      result = push_service.single_device_data_message(registration_id=registration_id, data_message=data_message)
return HttpResponse('success')
  • totally unrelated but GET request MUST be idempotent, so you want a POST request here. – bruno desthuilliers Apr 22 '20 at 10:54
  • wrt/ your issue, you have to check the ajax request's errors (check your browser's developers tools for a starting point, you will have a tab with a list of sent requests and their responses) and your server's responses (IOW the outputs of your devserver). The most probable issue is that you're using a relative url path for your ajax request and that it does'nt match your real url. If the script is part of your template, just use the builtin `{% url %}` tag to get the proper url. – bruno desthuilliers Apr 22 '20 at 11:01
  • Thanks for your time and suggestion, i'll try. :) – Samran Tariq Apr 22 '20 at 11:51

0 Answers0