0

I wish I can find some assistance with my code.

I have set up a Database on mySQL, and connected it to Django REST. Those both work as expected, and I can access the REST with Firefox REST Client with it returning the correct tables from the database.

I have started working for user interface with html and javascript and I have encountered a problem I am unable to solve. I am a student, and this is part of my school work, but unfortunately my teachers are unavailable at the moment due summer vacations and I am eager to continue my project. Hence I am askin for Your assistance.

As I have tested the Django REST through Firefox REST Client, I am sure the database and REST service is not at fault so here we come to my code.

I seem to be able to get connection to the REST Service, giving me code 200 and state 4 (pictures linked underneath)

ReadyStateChange + ReadyState console.logs

Picture 2 shows that my GET request gets stuck on OPTIONS, instead of executing the correct request.

200 OPTIONS

However I am unable to pull data out, giving me 'Content-Length: 0'.

Originally I thought the issue would be cross-domain request problem until my fellow student said he does not think it is, however he was unable to find solution for my code either.

I am trying to find reason and workaround for this error, and if you guys do have idea why this is happening I would deeply appriciate your help!

Here is my code:

<div id="demo"></div>
<script>
loadData() //function kutsu

function loadData(){

if (window.XMLHttpRequest) {
// code for modern browsers
xmlhttp = new XMLHttpRequest();
} else {
// code for old IE browsers
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} 

var url = "http://127.0.0.1:8000/vamkrs/";
xmlhttp.open("GET", url, true);
xmlhttp.withCredentials = true;
xmlhttp.setRequestHeader("Authorization", "Basic " + btoa("username:password"));
xmlhttp.send(); 

setTimeout(xmlhttp.onreadystatechange = function() {
console.log(this.status);
console.log(this.readyState);
if (this.readyState == 4 && this.status == 200) {
    var myData = JSON.parse(this.responseText); 
    document.getElementById("demo").innerHTML = myData.responseText(); }
},1500); 

/*
xmlhttp.onreadystatechange = function(){
console.log(this.status);
if (this.readyState == 4 && this.status == 200) {
var myData = JSON.parse(this.responseText); 
    document.getElementById("demo").innerHTML = myData.responseText();}
};  */
}
</script>

Ps. Sorry, English is not my native language so some spelling mistakes might have been made

Pss. First time posting here, I apologize if mistakes were made on the post

Honia
  • 1
  • 2
  • Well you here use a `GET` instead of `OPTIONS`. So the two are *not* identical. – Willem Van Onsem Jun 13 '18 at 07:17
  • I am sorry, but I do not quite understand the answer. As I understood, the 'OPTIONS' comes from GET not quite going through? What I wish to execute is indeed GET. Please correct me if I am wrong. I am fairly new to this sort of things and my understanding of the protocol and syntax might not be up to standards. – Honia Jun 13 '18 at 07:30
  • Well your second image shows that you did not make a `GET` request, but an `OPTIONS` request. – Willem Van Onsem Jun 13 '18 at 07:31
  • The request is send as "GET" but "stucks" at options at the network side, which is most likely related to the problem I am having with the code - there is no "OPTIONS" request made from my code side. – Honia Jun 13 '18 at 07:33
  • ah, this was not really clear for me based on the question. – Willem Van Onsem Jun 13 '18 at 07:37
  • I'll edit the question to reflect the issue! – Honia Jun 13 '18 at 07:39
  • If you're getting OPTIONS instead of GET, it could be a CORS issue. See for example [this question](https://stackoverflow.com/questions/1256593/why-am-i-getting-an-options-request-instead-of-a-get-request). – Daniel Roseman Jun 13 '18 at 08:39
  • For some reason I had thought, that GET would not even trigger CORS. Am I wrong with this? And if CORS was the issue, would not Firefox REST client also struggle getting out the tables? – Honia Jun 14 '18 at 04:51

0 Answers0