자바 스크립트에서 URL의 내용을 읽고 싶습니다. 내 도메인에 URL이 없으므로 상호 도메인에 액세스 할 수있는 중간 계층이 필요합니다. PHP 함수를 사용하여 URL을 읽고 jquery를 사용하여 결과를 javascript로 반환하려고했지만 작동하지 않았습니다. 여기 자바 스크립트에서 PHP를 사용하여 URL 내용 읽기
내 재판입니다 :<script>
$(document).ready(function() {
//httpQuery("http://asmary.dreameg.com/texttable.txt");
getOutput();
});
function getRequest() {
var req = false;
try {
// most browsers
req = new XMLHttpRequest();
} catch (e) {
// IE
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
// try an older version
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
return false;
}
}
}
return req;
}
function getOutput() {
var ajax = getRequest();
ajax.onreadystatechange = function() {
if (ajax.readyState == 4) {
document.getElementById('output').innerHTML = ajax.responseText;
}
};
ajax.open("GET", "phpjs_test.php", true);
ajax.send(null);
}
나는 완전히 새로운 해요 : 나는 "phpjs_test.php"
<?php
function get_data(){
$url='http://asmary.dreameg.com/texttable.txt';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
$content = htmlspecialchars($content);
curl_close($ch);
$content = nl2br($content);
return $content;
}
?>
라는 이름의 PHP 파일을 생성하고이 자바 스크립트 코드
그래서 PHP 함수조차도 맞지 않을지 모르겠다.
"그것은 작동하지 않았다"와 같은 JSON 배열을받을 수 없습니다 진단 매우 유용합니다 . – GordonM
AJAX가 응답 –
을 수신하려면'return'이 아닌'echo'를 사용해야하지만 echo get_data()와 같이 호출 할 수 있습니다. 잘 작동합니다. –