gpt4 book ai didi

php - 尝试将数据发送到本地主机 :8000 from localhost 时 ajax 调用不起作用

转载 作者:可可西里 更新时间:2023-11-01 13:16:16 24 4
gpt4 key购买 nike

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

XMLHttpRequest cannot load http://localhost:8000/project/login/uid=bimt;token=KAMWMS151UWP67Q. Origin 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);
}
});
});

最佳答案

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

了解更多:

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

或者,专门针对 django:

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

关于php - 尝试将数据发送到本地主机 :8000 from localhost 时 ajax 调用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17285895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com