1

I've been looking at how to integrate AJAX calls into a Python Django application and I'm somewhat new to both. I have been following the advice here:

https://www.quora.com/Can-I-execute-a-Python-function-from-JavaScript

Which led to this and this respectively for AJAX and Django advice.Both made pretty good sense to me. The desired end result is that a template in this fourth folder down (dashboard) call a function in a file called logic.py under the api folder above it:

enter image description here

In a JavaScript file hooked up to my django template, I have the following code I stole from the AJAX resource I listed above and made some light edits to:

window.onerror = function() {
  debugger;
}

// browser - safety
var request;

if (window.XMLHttpRequest) {
  request = new XMLHttpRequest();
} else if(window.ActiveXObject) { // ie
  try {
    request = new ActiveXObject('Msxml2.XMLHTTP');
  }
  catch(e) {
    try {
      request = new ActiveXObject('Microsoft.XMLHTTP');
    }
    catch(e) {}
  }
}

function step2() {
  console.log('something');
}


function testLogin() {
  request.open('POST', '../../../api/logic');
  // I've also been trying ^^^ logic.py, if it matters
  request.send(null);
  console.log('testLogin ran');

  step2();
}

request.onreadystatechange = function () {
  if(request.readystate === 4) {
    if(request.status === 200) {
      console.log(request.responseText);
    }
  }
}

It still is hooked up to a URL POST action in the django views, so when I hit the submit button in question I see the following two requests get generated: enter image description here

That is the correct filepath for that location:

enter image description here

So I'm wondering if I'm missing something either about the AJAX setup, or the way it needs to interact with Django, or some combination of the two. Other resources I've been consulting in looking into this:

Call Python function from Javascript code https://codehandbook.org/python-flask-jquery-ajax-post/ https://www.makeuseof.com/tag/python-javascript-communicate-json/ https://bytes.com/topic/javascript/answers/737354-how-call-python-function-javascript

And I originally started way earlier in the day so there were a few more, but needless to say I didn't see my issue immediately from looking at any of them. Any help is much appreciated.

JTK
  • 101
  • 3
  • 14
  • you should be using [django template URLs](https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#url) any time you are referring to a resource within your project – aydow Jun 20 '18 at 00:07
  • So, I do hear you: and normally i would. In this case, I'm not trying to go to that url, however: just call an API function so I can get a JSON web token from it. – JTK Jun 20 '18 at 00:39

1 Answers1

0

So this wound up being a quirk of the Django url setup, I can't believe I didn't figure it out but I hadn't been in that part of the code base for some time.

In our urls.py script, the place I was pinging (which was in the location of the URL described) had a redirect on it and for that reason, the traffic wasn't going to the place I thought.

JTK
  • 101
  • 3
  • 14