Forums

jQuery ajax call made to pyhonanywhere.com instead of user.pythonanywhere .com

Hi, I'm using Django and I have a simple ajax call made thru jQuery for an autocomplete field. The errors I get in my console are the following.

Origin https://gen709.pythonanywhere.com is not allowed by Access-Control-Allow-Origin. Status code: 404

and

XMLHttpRequest cannot load https://www.pythonanywhere.com/canadiannutrientfile/search/foodname/?lang=fr&term=p due to access control checks.

here is part of my ajax call

$("#food_name").keyup(function(){
console.log("keyup")    
$.ajax({url: "/canadiannutrientfile/search/foodname/",  
dataType: "json",
data:'lang=fr&term='+$(this).val(),

I ve also tried

$.ajax({url: "https://gen709.pythonanywhere.com/canadiannutrientfile/search/foodname/"

in both instances the actual URL used is

 https://www.pythonanywhere.com/canadiannutrientfile/search/foodname/?lang=fr&term=p

when I paste the good URL

https://gen709.pythonanywhere.com/canadiannutrientfile/search/foodname/?lang=fr&term=p

I guet the the expected server's response. Am I receiving the Access-Control-Allow-Origin because of the domaine switch and why is there a domaine switch in the first place

Thanks you very much

Did you reload your web app after you changed your code to $.ajax({url: "https://gen709.pythonanywhere.com/canadiannutrientfile/search/foodname/" ?

Yes I did to no avail

Are you sure that the browser is not caching your pages? Maybe do hard refresh.

Also did that same message

Access to XMLHttpRequest at '**https://www.pythonanywhere.com**/canadiannutrientfile/search/foodname/?lang=fr&term=p' from origin '**https://gen709.pythonanywhere.com**' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Anything else?

Perhaps you could view the source of your web page to see if somewhere you've accidentally put www.pythonanywhere.com where you meant to put gen709.pythonanywhere.com? You could also try using the browser's JavaScript debugger to step through and see if you can find out where it's coming from.

I've had a similar problem when running my program using chrome or firefox. When I run the same code on my phone it works as expected.

Check the network tab of your browser's developer tools to see what is happening to the requests.