이 문제는 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;
}
});
});
어디 보여줄 필요가 작동합니다 'data'와'text'를 정의하십시오. – charlietfl
data = $ (this) .serialize(); – rhill45
***이 좋은 질문을보세요 *** http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery?rq=1 – Musa