2013-08-08 4 views
7

아약스 게시물의 성공을 홈 페이지로 이동하고 싶습니다. 웬일인지 나는 그것을 잘못하고있다. 이 문제를 해결하기 위해 내가해야 할 일이 있습니까?Ajax 게시 후 리디렉션

window.APP_ROOT_URL = "<%= root_url %>"; 

아약스

$.ajax({ url: '#{addbank_bankaccts_path}', 
type: 'POST', 
beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#{form_authenticity_token}')}, 
dataType: "json", 
data: 'some_uri=' + response.data.uri , 
success: function(APP_ROOT_URL) { 
     window.location.assign(APP_ROOT_URL); 
    } 
}); 
+0

아약스가 성공으로 돌아오고 있습니까? –

+0

'window.location.href = APP_ROOT_URL' – abc123

+1

브라우저의 콘솔에 메시지가 보이십니까? – Uooo

답변

13
success: function(response){ 
         window.location.href = response.redirect; 
        } 

이 도움이 될 것입니다 희망과 내가 같은 문제

+2

(-1)로 만들고 싶습니다. – FakeRainBrigand

+0

@FakeRainBrigand 좋습니다. – Backtrack

+0

안녕하세요 downvoters는 코멘트를 남겨주세요 – Backtrack

6

당신은 리디렉션 상태 서버에서 JSON을 반환하고 URL을 리디렉션 할 수 있었다.

{"redirect":true,"redirect_url":"https://example.com/go/to/somewhere.html"} 

그리고 당신의 jQuery를 아약스 처리기는 아약스 설정에서 dataType: 'json'을 설정해야합니다

success: function (response) { 
    // redirect must be defined and must be true 
    if (response.redirect !== undefined && response.redirect) { 
     window.location.href = response.redirect_url; 
    } 
} 

주에서

. 희망이 도움이됩니다. 감사!

5

이유는 모르겠지만 window.location.href은 저에게 적합하지 않습니다. 대신 window.location.replace을 사용했는데 실제로 작동했습니다.

$('#checkout').click(function (e) { 
    e.preventDefault(); 
    $.ajax('/post/url', { 
     type: 'post', 
     dataType: 'json' 
    }) 
    .done(function (data) { 
     if (data.cartCount === 0) { 
      alert('There are no items in cart to checkout'); 
     } 
     else { 
      window.location.replace('/Checkout/AddressAndPayment'); 
     } 
    }); 
}); 
관련 문제