2014-01-29 2 views
0

아약스 요청의 결과를 요청한 것과 동일한 페이지에 게시하고 싶습니다. JQuery와의아약스를 사용하여 같은 페이지에 다시 게시하십시오.

AJAX이 여기에 변수 설정하도록되어

var ids = 1 

$(document).ready(function(){ 
$('#showbtn').on('click', function() { 
    $.ajax({ 
     url: "http://localhost:3000/teamplayers.json", 
     data: {'resolution':ids}, 
     type:"post", 
     dataType: "json", 
     cache: true, 
     success:function(){ 
      $('#test3').val(1); 
      alert("test 3"); 
      }, 
     error: function(error) { 
        alert("Failed " + console.log(error) + " " + error) 
        }   
     }); 
    }); 
}); 

:

# GET /teamplayers 
# GET /teamplayers.json 
def index 
    @teamplayers = Teamplayer.all 
    @fteams = Fteam.all 
    @teamplayer2 = 1 
    tid = params[:resolution] <-It should set here per the data section above 
    @ids = tid 
end 

불행히도는이 부분을 호출 Teamplayer 컨트롤러를 여기에 상호 작용하는 부분은 같은 컨트롤러

# POST /teamplayers 
# POST /teamplayers.json 
def create 
    @teamplayer = Teamplayer.new(teamplayer_params) 

    respond_to do |format| 
    if @teamplayer.save 
     format.html { redirect_to @teamplayer, notice: 'Teamplayer was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @teamplayer } 
    else 
     format.html { render action: 'new' } 
     format.json { render json: @teamplayer.errors, status: :unprocessable_entity } 
    end 
    end 
end 

그럼 같은 페이지에 게시하려면 어떻게해야합니까?

답변

0

당신은 동작이 아니라 create 동작이 부딪히는 것을 의미하는 ajax 인 POST입니다. 페이지를 조회 할 때 서버 로그 출력에서이를 볼 수 있어야합니다.

index 작업을 수행하려면 해상도 데이터 세트로 GET ajax 요청을 수행하여 원하는 데이터를 얻을 수 있어야합니다.

또한 실제로는 동적 데이터 인 경우이 Ajax 요청을 캐싱하지 않으려는 경우가있을 수 있지만 이는 귀하에게 달려 있습니다. 댓글에서


편집 :

당신이 아닌 일반적인 "데이터"와 같은 GET에 대한 쿼리 문자열로 매개 변수를 추가 할 필요가

. 댓글에서

$.ajax({ 
    url: "http://localhost:3000/teamplayers.json?resolution="+ids, 
    type:"GET", 
    dataType: "json", 
    success:function(){ 
     $('#test3').val(1); 
     alert("test 3"); 
     }, 
    error: function(error) { 
       alert("Failed " + console.log(error) + " " + error) 
       }   
    }); 
}); 

또 다른 편집 :

# GET /teamplayers 
# GET /teamplayers.json 
def index 
    @teamplayers = Teamplayer.all 
    @fteams = Fteam.all 
    @teamplayer2 = 1 
    tid = params.fetch(:resolution) { 0 } # make sure we got something 
    @ids = tid.to_i      # coerce string param to integer 
end 
+0

을하지만 난 가져 오기를 수행 할 때,이 페이지의 끝에있는 데이터 페이지를 호출

이 같은 시도 . http : // localhost : 3000/teamplayers/1 – user3240928

+0

@ user3240928 이것을 반영하는 답변이 업데이트되었지만'resolution'을 쿼리 문자열로 전달해야합니다. 논의. – mellowfish

+0

ok 이제 값을 전달한다는 것을 알지만 정수로 값을 전달하는 방법 – user3240928

관련 문제