尝试从localhost发送数据到localhost时,ajax调用不起作用 [英] ajax call not working when trying to send data to localhost:8000 from localhost

查看:482
本文介绍了尝试从localhost发送数据到localhost时,ajax调用不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个网站,其中我所做的后端是在django,前端使用php。问题是我正在尝试从 php(localhost) django(localhost:8000 )中的ajax调用尝试这样它给出以下错误

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.

ajax代码是

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


});

我如何从我的前端到loca的django后端进行ajax调用lhost:8000

how can i make a ajax call from my front end to the django backend on localhost:8000

推荐答案

因为端口不一样,它被认为是一个十字架请求。
您必须在要求的脚本中设置 Access-Control-Allow-Origin 头。

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:

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

这篇关于尝试从localhost发送数据到localhost时,ajax调用不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆