2012-06-22 15 views
0

我想如下使用相對URL與後Ajax調用:Django的:相對URL不交電話工作在阿賈克斯

當前URL路徑:

http://localhost:8000/customer/0/location/0/user/0/ 

我需要改變,以不同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; 
      }); 

對於我的獲取調用絕對和相對的網址,返回數據。

但是對於POST調用,當我使用相對url時,我得到內部服務器錯誤(絕對URL工作正常) 我不認爲它與CSRF有關,因爲在我看來,我還包括@csrf_exempt出於測試目的。 (我在請求中包含https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax

在使用相對URL進行郵政調用時,chrome調試器給了我以下錯誤消息。

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調用失敗? 在此先感謝...

回答

1

在Chrome網絡部分中,如果您有DEBUG=True,則可以預覽錯誤說明。

由於您在var relative= "../../line_group/addLine/1"末尾沒有斜線,因此CommonMiddleware可能會重定向您的請求。如果您想保留原有網址,請在項目設置中設置APPEND_SLASH = False

+0

感謝您的快速回復。缺少/導致錯誤。 但我很好奇爲什麼Get請求工作正常? – akotian

+1

POST變量不能被重定向到另一個頁面。 –

+0

感謝您的回答! – akotian