jQuery를 사용하여 텍스트 파일을 읽고 쓰려고합니다. 이미 파일을 읽는 함수를 작성했습니다. 하지만 할 수없는 것은 파일에 쓰는 것입니다. 나는이 개 파일을 가지고, read.txt
그리고 write.txt
코드와 같은 폴더에.
두 개의 jQuery 함수 (아래, 주변 서버 측 Perl 코드 포함)는 다음과 같습니다.
<!-- language: perl -->
my $script = qq{
\$(document).ready(function() {
\$("#readFile").click(function() {
\$.get('read.txt', function(data) {
\$("#container").val(data);
}, 'text');
});
});
\$.ajax({
url: './test.pl',
data: {
'myString' : "#cont"
},
success: function(data, textStatus, jqXHR) {
alert('string saved to file');
}
});
};
my $q = new CGI;
print $q->header;
print $q->start_html(
-title => "Read a File",
-style => {-src => 'css/ui-lightness/jquery-ui-1.10.3.custom.css" rel="stylesheet'},
-script => [
{-src => 'js/jquery-1.9.1.js'},
{-src => 'js/jquery-ui-1.10.3.custom.js'},
],
);
print $q->start_form;
print $q->textfield(
-style => 'font-family:verdana;width:300px;font-size:13px',
-id => 'container',
-value => '',
);
print $q->button(
-id => 'readFile',
-name => 'submit_form',
-value => 'Read',
);
print $q->textfield(
-style => 'font-family:verdana;width:300px;font-size:13px',
-id => 'cont',
-value => '',
);
print $q->submit(
-id => 'writeFile',
-name => 'submit_form',
-value => 'Write',
);
print $q->script($script);
print $q->end_html;
test.pl
use CGI ();
my $cgi = CGI->new;
print $cgi->header;
my $string = $cgi->param("myString");
open (FILE, ">", "./write.txt") || die "Could not open: $!";
print FILE $string;
close FILE;
보안상의 이유로 Javascript / jQuery를 사용하여 파일을 작성할 수 없습니다. 가능한 경우 사용자가 콘솔로 편집 할 파일 URL을 쉽게 변경하여 결과적으로 손상을 입힐 수 있기 때문입니다. 자세한 내용은 이 페이지를 참조하세요 .
문제를 해결하기 위해 가장 좋은 방법은 업데이트하려는 파일의 내용 인 ajax를 사용하여 원격 PHP 파일로 보내는 것입니다. 그런 다음이 파일이 업데이트를 수행합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다