2012-07-17 2 views
0

동적 문서 미리보기를 만들려고합니다. 사용자는 특정 문서 헤더 옵션을 입력 할 수 있으며 계정 정보를 기반으로 동적 헤더가있는 문서를 생성합니다. 결국 전체 PDF 미리보기를 원하지만 지금은 머리글 만 작업 중입니다.포스트 배열에 아약스 요청에서 json이 누락되었습니다.

내가하려는 것은 사용자가 작성할 수있는 양식의 페이지를 만든 다음 버튼을 눌러 헤더를 미리 봅니다.

$.ajaxSetup({ 
type:  'POST', 
timeout: 10000 
}); 

$("#preview_header").click(function(){ 
    var full_url = //appropriate URL 

    var preview_data = {  
     "wsid":    "default", 
     "page":    "default", 
     "banner_area1":  "default", 
     "banner_area2":  "default", 
     "banner_area3":  "default", 
     "uid":    "default", 
     "fid":    "default", 
     "cid":    "default", 
     "assignment_type": "default" 
    }; 

    preview_data.wsid    = $("#worksheet_picker").val(); 
    preview_data.page    = $("#page_picker").val(); 
    preview_data.banner_area1  = $("#banner_area1").val(); 
    preview_data.banner_area2  = $("#banner_area2").val(); 
    preview_data.banner_area3  = $("#banner_area3").val(); 
    preview_data.uid    = $("#member_uid").val(); 
    preview_data.fid    = $("#family_id").val(); 
    preview_data.assignment_type = 'family'; 
    preview_data.cid    = $("#class_id").val(); 

    var JSONText = JSON.stringify(preview_data); 
    alert('Full JSON - ' + JSONText); 

    $.ajax({ 
     async: true, 
     url: full_url, 
     data: { previewInfo : JSONText }, //Passes necessary form information 
     dataType: 'json', 
     success: function(output){ 
      var reply = output; 
      if (reply.status == "success"){ 
       $("#preview").attr("src", reply.image); 
      } else { 
       alert('Failed to create image preview of the assignment.'); 
      } 
     } 
    }); 
}); 

제가 알 수있는 한, 위의 방법은 정상적으로 작동합니다. 그것은 오른쪽 Codeigniter 페이지를 때리고 ajax 방법은 하드 코딩 된 이미지를 반환하도록 설정하면 잘 작동합니다. 아약스 잘 포맷하는 것 같지만 여기 경우에 내가 해당 값으로 양식을 작성할 때 출력 것입니다 :

Full JSON - {"wsid":"4","page":"1","banner_area1":"link1", 
"banner_area2":"link2","banner_area3":"link3", 
"uid":"1","fid":"1","assignment_type":"family"} 

을 그래서 첫째의가에 해당하는 컨트롤러 메소드에서 작업 된 내용으로 시작하자,

$data = array(
'status' => 'success', 
'image'  => //static image link 
); 

$this->output->set_content_type('text/javascript;charset=UTF-8'); 
echo json_encode($data); 

을하지만 때마다 나는 그렇게처럼 수정하려고 : 아약스는 응답

$preview_data = json_decode($this->input->post('previewInfo')); 

//Got this one 
mail('[email protected]', 'Start Email', 'Some email'); 
//Empty email 
mail('[email protected]', 'Dump Post', var_dump($_POST)); 
//Empty email 
mail('[email protected]', 'Post data', var_dump($preview_data)); 
//returns an email with 1 for body 
mail('[email protected]', 'Post data', print_r($this->input->post())); 
//returns an email with 1 for body 
mail('[email protected]', 'Post data', 
    print_r($this->input->post('previewInfo'))); 
//returns an email with 1 for body 
mail('[email protected]', 'Post data', print_r($preview_data)); 

$data = array(
'status' => 'success', 
'image'  => //static image link 
); 

$this->output->set_content_type('text/javascript;charset=UTF-8'); 
echo json_encode($data); 

수정 한 중 정적 데이터를 반환하지 않습니다. 그래서 게시물 배열이 제대로 초기화되지 않은 것 같습니다. 누구든지 그 벌레를 보았나요?

+1

출력 버퍼링을 사용하지 않는 한'var_dump'는 브라우저에만 출력 할 수 있습니다. 'print_r'는 브라우저가 아닌 반환 값으로 출력 할 수있게 해주는 두번째 매개 변수를 가지고 있습니다. Anyhoo, 결과적으로 반환 값은 유효한 JSON이 아니므로 Ajax 성공 콜백이 작동하지 않을 가능성이 있습니다. 'error' 콜백도 설정하면, 결과적으로 실행되어야합니다[email protected] Austin의 조언에 따라 Firebug 또는 Chrome 개발자 도구를 사용하여 보내고받는 내용을 확인해야합니다. – Gavin

답변

0

post 메소드로 Ajax 요청을 보내려면 $.ajax 옵션에서 type에서 POST으로 설정해야합니다.

또한 async은 기본적으로 true이므로 설정하지 않아도됩니다. 또한 successfail 대신에 .done.fail을 사용하는 것이 좋습니다. 곧 사용 중지 될 예정이므로

그래서 같은 오스틴 가빈 말했듯

1)

:

이 문제의 해결책을 초래할 2 문제
$.ajax({ 
    type: "POST", 
    url: full_url, 
    data: { previewInfo : JSONText }, //Passes necessary form information 
    dataType: 'json' 
}).done(function (output, textStatus, jqXHR) { 
    var reply = output; 
    if (reply.status == "success"){ 
     $("#preview").attr("src", reply.image); 
    } else { 
     alert('Failed to create image preview of the assignment.'); 
    } 
}).fail(function (jqXHR, textStatus, errorThrown) { 
    // now you have the XHR, text status, and error to debug with 
}); 
+0

나는 당신이 그랬던 것처럼 그것을 쓰려고 노력할 것이다. 그러나 나는 이것을 꼭대기에서했다. 죄송합니다. $ .ajaxSetup ({type : \t \t 'POST', timeout : \t 10000}); 조언 해 주셔서 감사합니다. 그에 따라 코드를 변경합니다. – Zigu

+0

또한, 아약스 요청의 데이터가 json 문자열로 전송되지는 않았지만 직접 게시해야하므로 서버 측에서 json_decode를 빼내보십시오. 무엇이 있는지보십시오. – Austin

+0

행동에 변화가 없음 :( – Zigu

0

있었다 위해서 var_dump 및 print_r의 출력되지 않아야 브라우저에. 해결책은 firefox extensions/chrome으로 디버깅하는 것이 었습니다.

2)

$preview_data = json_decode($this->input->post('previewInfo')); 

$preview_data = json_decode($this->input->post('previewInfo'), true); 

json_decode의 secondy 선택적 매개 변수는 연관 객체 또는 물건의 목록을 기대하고 있는지 방법을 알려주는 것이다 변경되었습니다. 그것이 일어날 때, 나는 안으로 읽고 있었고 연관 배열을 원했습니다.

관련 문제