php 尝试从 localhost 向 localhost:8000 发送数据时,ajax 调用不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17285895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:38:05  来源:igfitidea点击:

ajax call not working when trying to send data to localhost:8000 from localhost

phpajaxdjango

提问by user1001176

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

当我尝试从 to 进行 AJAX 调用php (localhost)django (localhost:8000),它会引发以下错误。

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

XMLHttpRequest 无法加载 http://localhost:8000/project/login/uid=bimt;token=KAMWMS151UWP67Q。Access-Control-Allow-Origin 不允许使用Origin http://localhost

$(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);
        }
     });
});

回答by Tamás Pap

Because the port is not the same, it's considered a cross origin request. You must set a Access-Control-Allow-Originheader in the script you are requesting.

因为端口不一样,所以被认为是跨域请求。您必须Access-Control-Allow-Origin在您请求的脚本中设置一个标头。

Learn more:

了解更多:

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

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

or, specifically for django:

或者,专门针对 Django:

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

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

回答by 7stud

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

1)php不做ajax请求。php在服务器端执行;javascript在客户端执行,是js向服务器发出ajax请求。

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

2) js 不允许您向与获取当前页面的主机不同的主机发出 ajax 请求。

回答by StackSlave

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 a relative path.

您应该只使用相对路径。

回答by Kunal

I swapped my internal IP (192.168.1.x) in the place of localhost and was able to call a XMLHttpRequest on it. This should do the trick without messing with cross origin policy.

我在本地主机的位置交换了我的内部 IP (192.168.1.x),并且能够在其上调用 XMLHttpRequest。这应该可以在不干扰跨源策略的情况下解决问题。

回答by Rafa Jaques

AJAX, by default, can't make cross-domain requests.

AJAX,默认情况下,不能进行跨域请求。

You should refer to this link.

你应该参考这个链接