Django: relative url not working with post calls in ajax
I am trying to use relative url with a post ajax call as follows:
Current url path:
http://localhost:8000/customer/0/location/0/user/0/
I need to change to different directoy.
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;
});
For my get calls both, absolute and relative url, return data.
But for POST call, when I use relative url, I get internal server error.(absolute URL works fine) I do not think it has got to do with CSRF, as in my view I have also include @csrf_exempt for test purposes. (I have included https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax in request)
The chrome debugger gives me the following error message on the post call with relative 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
However as you can see it does provide me the complete url link, which I want to access. And when I click directly on the link, I get the data of the page.
The view is really simple:
@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)
Any body has any suggestion, as to why the relative url path fails on POST call? Thanks in advance..
In Chrome Network section you can preview the error explanation if you have DEBUG=True
.
As you have no slash at the end of var relative= "../../line_group/addLine/1"
, it might be that CommonMiddleware redirects your request. Set APPEND_SLASH = False
in project settings if you want to keep your URL as it is.