0

Im using django channels for implementing instant messaging app in my project.The message box does not take up th entire screen ,so im trying to implement it using ajax.The problem im facing is that in my url field in the ajax is prepending with http://locahost .I dont want this since im using ASGI and django channels with ws://

I have tried prepending the url with a "/"

var wsStart = 'ws://';
if (loc.protocol == 'https:'){
    wsStart ='wss://';
 }
var endpoint = wsStart + loc.host;

$.ajax({
  type: 'POST',
  url:"/"+endpoint+"/messages/"+username+"/",
  data: {
  'username': username,
   csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val()
   },
   success: function (res, status) {
    console.log("RESOPONSE",res);
    },
    error: function (res) {
    console.log(res.status);
   }
  });

I want the url to be ws://localhost:8000/messages/

what i get now is
http://localhost:8000/ws://localhost:8000/messages/mohitharshan123/

Mohit Harshan
  • 1,916
  • 1
  • 18
  • 41

2 Answers2

1

The problem is that you start the url with "/", and that means the current base url on whish you append other path. Specify the url first ws://localhost and after append what you want.

jalanga
  • 1,456
  • 2
  • 17
  • 44
1

Actually you can't use WebSocket with AJAX or CORS, it won't work.