Ajax call does not work when trying to send data to localhost: 8000 from localhost

When I try to make an AJAX call from php (localhost) to django (localhost:8000) , it throws the following error.

XMLHttpRequest cannot load http: // localhost: 8000 / project / login / uid = bimt; token = KAMWMS151UWP67Q . Source http: // localhost is not allowed by Access-Control-Allow-Origin.

 $(document).on('click', '.login', function(event) { var username = $('#username').val(); var token = $('#token').val(); $.ajax({ type: "POST", url: "http://localhost:8000/project/login/uid=" + username + ";token=" + token, success: function (html) { alert(html); } }); }); 
+6
source share
5 answers

Since the port is not the same, it is considered a cross-lookup request. You must set the Access-Control-Allow-Origin header in the script request.

More details:

http://enable-cors.org/server.html

or in particular for django:

http://chase-seibert.imtqy.com/blog/2012/01/27/using-access-control-allow-origin-to-make-cross-domain-post-requests-from-javsacript.html

+8
source

1) php does not make ajax requests. php runs on the server side; javascript is executed on the client side, and it is js that makes ajax requests to the server.

2) js does not allow you to make ajax requests to a different host than the one from which the current page was received.

+1
source

AJAX cannot perform cross-domain requests by default.

You must refer to this link .

0
source

Try something like:

 $('#yourClickButtonId').click(function(){ $.ajax({ type: 'POST', url: 'project/login?uid='+$('#usename')+'&token='+$('#token'), success: function(data){ alert(data); } }); }); 

You should just use the relative path.

0
source

I replaced my internal IP (192.168.1.x) instead of the local host and was able to call XMLHttpRequest on it. This should do the trick without delving into cross-origin policies.

0
source

Source: https://habr.com/ru/post/948020/


All Articles