Ignore http:localhost from ajax url












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/










share|improve this question





























    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/










    share|improve this question



























      0












      0








      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/










      share|improve this question
















      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/







      ajax django django-channels






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 28 '18 at 14:53







      Mohit Harshan

















      asked Dec 28 '18 at 14:47









      Mohit HarshanMohit Harshan

      1329




      1329
























          2 Answers
          2






          active

          oldest

          votes


















          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.






          share|improve this answer
























          • it does the same after removing the "/"

            – Mohit Harshan
            Dec 28 '18 at 15:06











          • Add the url string without parameters, hard string, and it must work.

            – jalanga
            Dec 28 '18 at 15:07











          • Sorry..Now it works but now im stuck with another error jquery-latest.js:9631 Access to XMLHttpRequest at 'ws://localhost:8000/messages/mohitharshan123/' from origin 'htttp://localhost:8000' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

            – Mohit Harshan
            Dec 28 '18 at 15:08













          • Set allowed hosts and cors in django settings.

            – jalanga
            Dec 28 '18 at 15:09











          • allowed hosts has already been set to '*' .For the cors should i install django-cors-headers?

            – Mohit Harshan
            Dec 28 '18 at 15:12



















          1














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






          share|improve this answer
























          • please tell me any other way to implement this then

            – Mohit Harshan
            Dec 28 '18 at 16:50











          • Just create the web socket I don't see where the problem is even if the message box doesn't take the entire screen

            – SP SP
            Jan 2 at 18:07











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53960293%2fignore-httplocalhost-from-ajax-url%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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.






          share|improve this answer
























          • it does the same after removing the "/"

            – Mohit Harshan
            Dec 28 '18 at 15:06











          • Add the url string without parameters, hard string, and it must work.

            – jalanga
            Dec 28 '18 at 15:07











          • Sorry..Now it works but now im stuck with another error jquery-latest.js:9631 Access to XMLHttpRequest at 'ws://localhost:8000/messages/mohitharshan123/' from origin 'htttp://localhost:8000' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

            – Mohit Harshan
            Dec 28 '18 at 15:08













          • Set allowed hosts and cors in django settings.

            – jalanga
            Dec 28 '18 at 15:09











          • allowed hosts has already been set to '*' .For the cors should i install django-cors-headers?

            – Mohit Harshan
            Dec 28 '18 at 15:12
















          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.






          share|improve this answer
























          • it does the same after removing the "/"

            – Mohit Harshan
            Dec 28 '18 at 15:06











          • Add the url string without parameters, hard string, and it must work.

            – jalanga
            Dec 28 '18 at 15:07











          • Sorry..Now it works but now im stuck with another error jquery-latest.js:9631 Access to XMLHttpRequest at 'ws://localhost:8000/messages/mohitharshan123/' from origin 'htttp://localhost:8000' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

            – Mohit Harshan
            Dec 28 '18 at 15:08













          • Set allowed hosts and cors in django settings.

            – jalanga
            Dec 28 '18 at 15:09











          • allowed hosts has already been set to '*' .For the cors should i install django-cors-headers?

            – Mohit Harshan
            Dec 28 '18 at 15:12














          1












          1








          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 28 '18 at 15:01









          jalangajalanga

          266218




          266218













          • it does the same after removing the "/"

            – Mohit Harshan
            Dec 28 '18 at 15:06











          • Add the url string without parameters, hard string, and it must work.

            – jalanga
            Dec 28 '18 at 15:07











          • Sorry..Now it works but now im stuck with another error jquery-latest.js:9631 Access to XMLHttpRequest at 'ws://localhost:8000/messages/mohitharshan123/' from origin 'htttp://localhost:8000' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

            – Mohit Harshan
            Dec 28 '18 at 15:08













          • Set allowed hosts and cors in django settings.

            – jalanga
            Dec 28 '18 at 15:09











          • allowed hosts has already been set to '*' .For the cors should i install django-cors-headers?

            – Mohit Harshan
            Dec 28 '18 at 15:12



















          • it does the same after removing the "/"

            – Mohit Harshan
            Dec 28 '18 at 15:06











          • Add the url string without parameters, hard string, and it must work.

            – jalanga
            Dec 28 '18 at 15:07











          • Sorry..Now it works but now im stuck with another error jquery-latest.js:9631 Access to XMLHttpRequest at 'ws://localhost:8000/messages/mohitharshan123/' from origin 'htttp://localhost:8000' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

            – Mohit Harshan
            Dec 28 '18 at 15:08













          • Set allowed hosts and cors in django settings.

            – jalanga
            Dec 28 '18 at 15:09











          • allowed hosts has already been set to '*' .For the cors should i install django-cors-headers?

            – Mohit Harshan
            Dec 28 '18 at 15:12

















          it does the same after removing the "/"

          – Mohit Harshan
          Dec 28 '18 at 15:06





          it does the same after removing the "/"

          – Mohit Harshan
          Dec 28 '18 at 15:06













          Add the url string without parameters, hard string, and it must work.

          – jalanga
          Dec 28 '18 at 15:07





          Add the url string without parameters, hard string, and it must work.

          – jalanga
          Dec 28 '18 at 15:07













          Sorry..Now it works but now im stuck with another error jquery-latest.js:9631 Access to XMLHttpRequest at 'ws://localhost:8000/messages/mohitharshan123/' from origin 'htttp://localhost:8000' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

          – Mohit Harshan
          Dec 28 '18 at 15:08







          Sorry..Now it works but now im stuck with another error jquery-latest.js:9631 Access to XMLHttpRequest at 'ws://localhost:8000/messages/mohitharshan123/' from origin 'htttp://localhost:8000' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

          – Mohit Harshan
          Dec 28 '18 at 15:08















          Set allowed hosts and cors in django settings.

          – jalanga
          Dec 28 '18 at 15:09





          Set allowed hosts and cors in django settings.

          – jalanga
          Dec 28 '18 at 15:09













          allowed hosts has already been set to '*' .For the cors should i install django-cors-headers?

          – Mohit Harshan
          Dec 28 '18 at 15:12





          allowed hosts has already been set to '*' .For the cors should i install django-cors-headers?

          – Mohit Harshan
          Dec 28 '18 at 15:12













          1














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






          share|improve this answer
























          • please tell me any other way to implement this then

            – Mohit Harshan
            Dec 28 '18 at 16:50











          • Just create the web socket I don't see where the problem is even if the message box doesn't take the entire screen

            – SP SP
            Jan 2 at 18:07
















          1














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






          share|improve this answer
























          • please tell me any other way to implement this then

            – Mohit Harshan
            Dec 28 '18 at 16:50











          • Just create the web socket I don't see where the problem is even if the message box doesn't take the entire screen

            – SP SP
            Jan 2 at 18:07














          1












          1








          1







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






          share|improve this answer













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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 28 '18 at 16:15









          adonis_97adonis_97

          264




          264













          • please tell me any other way to implement this then

            – Mohit Harshan
            Dec 28 '18 at 16:50











          • Just create the web socket I don't see where the problem is even if the message box doesn't take the entire screen

            – SP SP
            Jan 2 at 18:07



















          • please tell me any other way to implement this then

            – Mohit Harshan
            Dec 28 '18 at 16:50











          • Just create the web socket I don't see where the problem is even if the message box doesn't take the entire screen

            – SP SP
            Jan 2 at 18:07

















          please tell me any other way to implement this then

          – Mohit Harshan
          Dec 28 '18 at 16:50





          please tell me any other way to implement this then

          – Mohit Harshan
          Dec 28 '18 at 16:50













          Just create the web socket I don't see where the problem is even if the message box doesn't take the entire screen

          – SP SP
          Jan 2 at 18:07





          Just create the web socket I don't see where the problem is even if the message box doesn't take the entire screen

          – SP SP
          Jan 2 at 18:07


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53960293%2fignore-httplocalhost-from-ajax-url%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas