从本地主机8000:试图将数据发送到本地主机时,Ajax调用不工作 [英] ajax call not working when trying to send data to localhost:8000 from localhost
问题描述
我在做一个网站,其中后端我已经是在Django和前端使用PHP。问题是我想使从 A Ajax调用PHP(本地)
到 Django的(本地主机:8000
每当我尝试这样它给MES以下错误
i am making a website in which the back end i have made is in django and the front end uses php . The problem is i am trying to make a ajax call from php (localhost)
to django (localhost:8000
whenever i try so it gives mes the following error
XMLHttpRequest cannot load http://localhost:8000/project/login/uid=bimt;token=KAMWMS151UWP67Q. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
阿贾克斯code是
the ajax code is
$(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);
}
});
});
我如何才能从我的前端Ajax调用本地主机Django的后端:8000
how can i make a ajax call from my front end to the django backend on localhost:8000
推荐答案
由于该端口是不一样的,它被认为跨起源请求。
您必须设置访问控制 - 允许 - 原产地
在脚本的头部,你的请求。
Because the port is not the same, it's considered a cross origin request.
You must set a Access-Control-Allow-Origin
header in the script you are requesting.
了解更多:
http://enable-cors.org/server.html
,或专门为Django的:
or, specifically for django:
<一个href="http://chase-seibert.github.io/blog/2012/01/27/using-access-control-allow-origin-to-make-cross-domain-post-requests-from-javsacript.html" rel="nofollow">http://chase-seibert.github.io/blog/2012/01/27/using-access-control-allow-origin-to-make-cross-domain-post-requests-from-javsacript.html
这篇关于从本地主机8000:试图将数据发送到本地主机时,Ajax调用不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!