0

我正在尝试将相对 url 与 post ajax 调用一起使用,如下所示:

当前网址路径:

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,返回数据。

但是对于 POST 调用,当我使用相对 url 时,我得到内部服务器错误。(绝对 URL 工作正常)我认为它与 CSRF 无关,因为在我看来,我还包括 @csrf_exempt 用于测试目的。(我在请求中包含了https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax )

chrome 调试器在 post call 中使用相对 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 调用中失败?提前致谢..

4

1 回答 1

1

在 Chrome 网络部分,您可以预览错误说明(如果有DEBUG=True)。

由于 末尾没有斜线var relative= "../../line_group/addLine/1",可能是 CommonMiddleware 重定向了您的请求。APPEND_SLASH = False如果您想保持 URL 不变,请在项目设置中设置。

于 2012-06-22T14:27:35.540 回答