Django:相对url在ajax中不适用于post调用
我正在尝试将相对url与后ajax调用结合使用,如下所示: 当前url路径:Django:相对url在ajax中不适用于post调用,ajax,django,django-urls,django-csrf,Ajax,Django,Django Urls,Django Csrf,我正在尝试将相对url与后ajax调用结合使用,如下所示: 当前url路径: http://localhost:8000/customer/0/location/0/user/0/ 我需要换个导演 var absolute = "http://localhost:8000/customer/0/location/0/line_group/addLine/2/";//+phone_id; var relative= "../../line_group/addLine/1"
http://localhost:8000/customer/0/location/0/user/0/
我需要换个导演
var absolute = "http://localhost:8000/customer/0/location/0/line_group/addLine/2/";//+phone_id;
var relative= "../../line_group/addLine/1"
$.get(relative,function(data){
//this works
alert(data);
});
$.ajax({
type: "POST",
url: relative,
data: "test=test1",
error:function(data){
//throws error when using relative path
alert('error');
},
success:function(data){
// works fine when using absolute path
alert('success');
}
});
//same thing using just post
$.post(relative,test,function(data){
//Error on relative path
alert(data);
return false;
});
对于我的get调用(绝对url和相对url),返回数据
但对于POST调用,当我使用相对url时,我会得到内部服务器错误。(绝对url工作正常)
我认为这与CSRF无关,因为在我看来,出于测试目的,我还包括@CSRF_豁免。
(我已包括在请求中)
chrome调试器在post调用中使用相对URL向我提供以下错误消息
Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR) http://localhost:8000/customer/0/location/0/line_group/addLine/1
但是,正如您所看到的,它确实为我提供了完整的url链接,我想访问它。
当我直接点击链接时,我得到了页面的数据
观点非常简单:
@csrf_exempt
def addNewLine(request, customer_id, location_id, phone_id,**kwargs):
error_msg = u"No POST data sent."
context = {}
return render_to_response('line_group/mytest.html', context)
对于为什么相对url路径在POST调用时失败,任何机构都有任何建议吗?
提前感谢。在Chrome网络部分,如果您有
DEBUG=True
,您可以预览错误解释
由于
var relative=“../../line_group/addLine/1”
的末尾没有斜杠,可能是CommonMiddleware重定向了您的请求。如果您想保持URL不变,请在项目设置中设置APPEND\u SLASH=False
。谢谢您的快速回复。丢失的/正在导致错误。但我很好奇为什么Get请求工作正常??POST变量无法重定向到另一个页面。