2015-01-09 1 views
0

이 문제는 ajax를 사용할 때 데이터 메서드가 어떻게 작동하는지 잘 이해하지 못하기 때문에 발생합니다. 여기에 아약스에 대한 다른 질문을 많이 보았지만이 부분은 나를 위해 클릭하지 않습니다.jquery ajax를 사용한 파일 업로드의 데이터 메서드 설정

예를 들어, 저는 폼 데이터 (여러 텍스트 필드와 하나의 파일 업로드 포함)를 php에 보내고 있습니다. 그런 다음 서버 스크립트는 필드 데이터와 파일 URL을 SQL DB에 삽입 한 다음 해당 값을 모두 json으로 인코딩 된 배열로 반환합니다.

현재 아약스 스크립트는 아래에 표시되어 있으며 실행시 파일 업로드 부분을 제외하고는 모두 작동합니다. 파일이 서버에 저장되지 않고 URL이 다시 돌아 오지 않습니다. 내가 변경하는 경우 :

data: data, 

data: text, 

파일 업로드 파일이 저장됩니다, 작동하지만 다음 JSON 배열이 페이지로 돌아올 때 내가 배열 데이터를 표시하는 흰색 화면을 얻을

에 (양식 데이터가 나오는 페이지로 돌아 가지 않습니다).

왜 이런가요? 내 파일 업로드가 작동하고 데이터가 전송 된 페이지로 돌아갈 수 있도록 허용하는 올바른 데이터 방법은 무엇입니까?

$("document").ready(function() { 
$(".data-form").submit(function() { 
     var data = new FormData(); 
$.each(files, function(key, value) 
    { data.append(key, value); 
}); 
    if (confirm("\t\t\tAre you ready to sumbmit this listing?\nYou can always edit the listing after going to the menu tab - edit listing."))  { 
     $.ajax({ 
      type: "POST", 
      dataType: "json", 
      url: "add-list.php", 
      data: data, 
      processData: false, 
      contentType: false, 
      success: function(response) { 
         if (response.success) { 
          $("#modal1").modal('hide'); 
         $("#add_frame").show(); 
         $("#newbutt").show(); 
         $(".atitle").html('<a href="' + response.ad_linka + '" target="_blank">' + response.titlea + '</a>'); 
         $(".acomment").html(response.commenta); 
         $(".aaddress").html(response.addressa); 
         $(".asale-price").html(response.sale_pricea); 
         $(".alease-price").html(response.lease_pricea); 
         $(".alot-size").html(response.lot_sizea); 
         $(".abuilding-size").html(response.build_sizea); 
         $(".azoning").html(response.zoninga); 
         } 
         else { 
          console.log("An error has ocurred: sentence: " + response.sentence + "error: " + response.error); 
         } 
        }, 
        error: function() { 
         alert("An Error has ocurred contacting the server. Please contact your system administrator"); 
        } 
       }); 
     return false; 
    } 
}); 
}); 
+1

어디 보여줄 필요가 작동합니다 'data'와'text'를 정의하십시오. – charlietfl

+0

data = $ (this) .serialize(); – rhill45

+1

***이 좋은 질문을보세요 *** http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery?rq=1 – Musa

답변

1

당신은 당신의 데이터 처리 중지 jQuery를 말할 필요가

 processData: false, // Don't process the files 
    contentType: false, // defaults to 'application/x-www-form-urlencoded; charset=UTF-8' 

당신은 당신이 데이터를 구성하는 방법을 보내지 않았지만, 이런 일이

var data = new FormData(); 
    $.each(files, function(key, value) 
     { data.append(key, value); 
    }); 
+0

그래서 데이터를 정의하기 위해 serialize를 사용하고있었습니다. 예, 나는 그것을 포함해야했다). 그래서 원래의 질문에 넣었던 변경 사항이 파일 업로드를 허용하지만 페이지가 원래 페이지로 돌아 오지는 않습니다. 배열을 보여주는 화이트 페이지로 이동합니다. – rhill45

+0

PHP에서 POST를 어떻게 처리합니까? pdo와 bindparam이있는 – silviud

+0

ex $ stmt-> bindParam (': title', $ _POST [ 'title']); 모든 데이터가 다시 표시됩니다 (브라우저 도구와 화면에서 볼 수 있음). 데이터 서식 지정 방법을 묻는 메시지는 – rhill45

관련 문제