2012-06-22 5 views
0
다음과 같이 내가 게시물 아약스 호출 상대 URL을 사용하려고

:장고 : 상대 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; 
      }); 

내 경우 get absolute absolute 및 relative url에 대해 데이터를 반환하십시오.

하지만 POST 호출의 경우 상대 URL을 사용하면 내부 서버 오류가 발생합니다. (절대 URL 작동) 내보기와 마찬가지로 @csrf_exempt가 CSRF와 관련 있다고 생각하지 않습니다. 테스트 목적으로. (요청시 https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax을 포함 시켰습니다.)

Chrome 디버거에서 상대방 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 호출에 실패하는 이유에, 어떤 제안이있다? 미리 감사드립니다.

답변

1

크롬 네트워크 섹션에서 DEBUG=True 인 경우 오류 설명을 미리 볼 수 있습니다.

var relative= "../../line_group/addLine/1" 끝에 슬래시가 없으므로 CommonMiddleware가 요청을 리디렉션 할 수 있습니다. URL을 그대로 유지하려면 프로젝트 설정에서 APPEND_SLASH = False을 설정하십시오.

+0

빠른 답장을 보내 주셔서 감사합니다. /가 없기 때문에 오류가 발생했습니다. 하지만 Get 요청이 잘 작동하는지 궁금합니다. – akotian

+1

POST 변수를 다른 페이지로 리디렉션 할 수 없습니다. –

+0

답변 해 주셔서 감사합니다 !! – akotian

관련 문제