PHP 파일에 대한 ajax 호출을 수행하면 다음 코드가 있습니다.
$arr = array('oldid' => 18258, 'newid' => 98765);
echo json_encode($arr);
그런 다음 JS ..
$.get("/admin/getdata.php", {
"otherdata": $("#mydata").val()
}).done(function (data) {
console.log(data); // prints {"oldid":"18258","newid":"98765"}
console.log(data.oldid); // prints undefined
console.log(data.newid); // prints undefined
});
js에서 키에 액세스하고 싶을 때 json_encode가 맞지 않습니까 data.oldid
??
PHP는 맞지만 JSON을 기대한다고 JS에 알려야합니다. 이렇게 할 수 있습니다
$.getJSON("/admin/getdata.php", {
"otherdata": $("#mydata").val()
}).done(function (data) {
console.log(data.oldid);
});
또는 이렇게
$.get("/admin/getdata.php", {
"otherdata": $("#mydata").val()
}).done(function (data) {
data = JSON.parse(data);
console.log(data.oldid);
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다