2011-10-11 3 views
0

외부 파일에서 응답을 받으려고합니다. PHP 파일에는 현재 한 개의 변수 만 표시되어 있으므로 #code div로 표시하고 싶습니다. 어떻게해야합니까?Jquery 및로드 함수

$(function(){ 
      $('#submit').click(function(e){ 
       var length = $('#number').val(); 
       var type = $('#type').val(); 
       var submit = "submit"; 
       var url = 'public/php/codegenerator.php'; 
       var data = "length=" + length + "&type=" + type + "&submit=" + submit; 
       console.log(data); 

       $('#code').load(url, data, function(response){ 
        $(this).response.hmtl(); 
       }); 

      e.preventDefault(); 
      }); 
     }); 

답변

1

load 그래서이로 변경이

$('#code').load(url, data, function(response){ 
     $(this).html(response); 
}); 

또는

$('#code').load(url, data); 
+0

이것은 작동하고 있습니다. PHP 파일에서 변수를 가져 오는 방법은 무엇입니까? – Sasha

+0

로드는 서버에서 가져온 가장 간단한 데이터입니다. 주로 전체 HTML 세트를 가져 와서 페이지에 삽입하는 데 사용됩니다. 변수를 다시 가져 와서 호출하려는 경우 response.myphpvariable,이 방법을 http://api.jquery.com/jQuery.getJSON/에서 확인하시기 바랍니다. PHP 변수를 인쇄하는 대신 JSON으로 인코딩하고 js 코드에서 원하는대로 사용할 수 있습니다 – realshadow

0

작동합니다 PHP 코드가 공동 작업자를 돌려 보내는 경우 올바른 일 텍스트 유형 "text/html". PHP 코드에서 응답의 content-type을 설정해보십시오. 내용 유형을 변경하는 방법을 알 수 없거나 원치 않는 경우 jcode.get() 메서드를 사용하고 #Code 요소의 TEXT (html이 아닌)를 $("#Code).text(yourResponse);

으로 변경하십시오. 전체 응답을 보려면 xhr.responseText에 액세스 해보십시오. 이것은 무엇이 잘못 되었는가를 알아 내는데 사용할 때 매우 귀중한 도구입니다 :

$.ajaxSetup ({ 
error: function(x, e){ 

     alert("The Response:" + x.responseText); 

}); 
0

이 코드 평화 그래서

<div id="success"></div> 
<div id="error"></div> 

<script> 
$("#success").load(url, data, function(response, status, xhr) { 
    if (status == "error") { 
    var msg = "error: "; 
    $("#error").html(msg + xhr.status + " " + xhr.statusText); 
    } 
}); 
</script> 
0

을 시도하려고하면 주로하고있는

$('#code').load(url, data, function() { 
    alert('success'); 
});