[英]How do I access the data returned in an AJAX call with vanilla JavaScript?
I wrote my first AJAX call last week and got it to work, but now I'm writing a different AJAX call and need to access the data returned, not just change something onSuccess.上周我写了我的第一个 AJAX 调用并让它工作,但现在我正在写一个不同的 AJAX 调用,需要访问返回的数据,而不仅仅是改变一些 onSuccess。
<script type="text/javascript">
function showMessage(jobid) {
let result;
new Request.JSON({
url: '<?php echo site_url('job/getDiscrepancyByJobId/'); ?>/' + jobid,
dataType: 'text',
onSuccess: function(data) {
console.log(data);
}
}).send();
}
</script>
When the script above is called, I get nothing output to the console.当上面的脚本被调用时,我没有得到任何 output 到控制台。 I tried doing this:我试过这样做:
<script type="text/javascript">
function showMessage(jobid) {
let request = new XMLHttpRequest();
request.open('GET', '<?php echo site_url('job/getDiscrepancyByJobId'); ?>/' + jobid, true);
request.onload = function() {
if (this.status >= 200 && this.status < 400) {
console.log('data: ' + JSON.stringify(this.response));
} else {
// Error
}
};
request.send();
}
</script>
but that just outputs data: ""
to the console.但这只是将data: ""
输出到控制台。
This is the function that is called:这是 function 被称为:
public function getDiscrepancyByJobId($jobid) {
return $this->MJob->getJobChangeLog($jobid);
}
And MJob->getJobChangeLog($jobid) is:而 MJob->getJobChangeLog($jobid) 是:
function getJobChangeLog($jobid, $limit = 10) {
if (empty($jobid)) {
return false;
}
$Q = $this->read_db->query('select timestamp, ifnull(t.name, ifnull(u.name, "")) as name, uri, description, postdata, marginchange
from jobchangelog l
left join user u on u.id=l.userid
left join transcriber t on t.id=u.transcriberid
where jobid=' . intval($jobid) . '
order by timestamp desc
limit ' . intval($limit));
return array_map(function($a){ $a['postdata'] = @unserialize($a['postdata']); return $a; }, $Q->result_array());
}
The information I'm looking for is in the @unserialize($a['postdata'])
part and I don't know how to access that either.我正在寻找的信息在@unserialize($a['postdata'])
部分中,我也不知道如何访问它。
Any help would be great.任何帮助都会很棒。 Thank you in advance for your patience.预先感谢您的耐心等待。
I changed the getDiscrepancyByJobId function to this:我将 getDiscrepancyByJobId function 更改为:
public function getDiscrepancyByJobId($jobid) {
$discrepancy = $this->MJob->getJobChangeLog($jobid);
echo $discrepancy[0]['postdata']['message'];
}
And I changed the AJAX request to this:我将 AJAX 请求更改为:
function showMessage(jobid) {
let request = new XMLHttpRequest();
let jid = jobid;
request.open('GET', '<?php echo site_url('job/getDiscrepancyByJobId'); ?>/' + jobid, true);
request.onload = function() {
if (this.status >= 200 && this.status < 400) {
console.log(decodeURIComponent(this.response));
} else {
// Error
}
};
request.send();
}
Thanks for the help!谢谢您的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.