I've been developing a chatbot system, and I'm encountering an issue where I receive an error message every time I send a message and expect a response back.
What's puzzling is that the error message varies depending on whether I run the site on localhost port 80 or 5500. Additionally, running it on localhost:5500 doesn't require an Apache server, whereas port 80 (localhost) does.
If I run it on localhost (port 80), I encounter:
POST http://localhost/get-response/ 404 (Not Found)
On the other hand, if I run it on localhost:5500, I see:
POST http://localhost:5500/get-response/ 405 (Method Not Allowed)
In my code snippet chatbot.js, there appears to be an error causing this issue:
fetch("/get-response/", { //<--- error
body: JSON.stringify({'message': message['text']}),
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'user-agent': 'Mozilla/4.0 MDN Example',
'content-type': 'application/json'
},
method: 'POST',
mode: 'cors',
redirect: 'follow',
referrer: 'no-referrer',
})
.then(response => response.json()).then((json) => {
this.messages.push(json['message'])
})
Furthermore, in urls.py and views.py, the routing and handling of the POST request are configured as follows:
urlpatterns = [
url('', Index),
path('get-response/', get_response),
]
@csrf_exempt
def get_response(request):
# Code for handling the POST request
If I run into errors with the post data, how do I determine which specific error message to troubleshoot?