当我尝试从to进行AJAX调用php (localhost)时django (localhost:8000),它将引发以下错误。
php (localhost)
django (localhost:8000)
XMLHttpRequest无法加载 http:// localhost:8000 / project / login / uid = bimt; token = KAMWMS151UWP67Q。Access- Control-Allow-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); } }); });
由于端口不同,因此将其视为跨源请求。您必须Access-Control-Allow-Origin在所请求的脚本中设置标题。
Access-Control-Allow-Origin
学到更多:
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