我第一次不清楚ajax
请求。
当我单击链接时,应该会出现另一个链接
这就是我想要做的(简化),但问题是当它出现里面的 JS 不想运行时
HTML:
<html><body>
<div id="result"> </div>
<a href="javascript:suggest('result'); return true;">click to see the link</a>
</body></html>
JavaScript
function evalResponse (request)
{
try
{
//var responseText = request.responseText.replace (/\n/g, " ");
var responseText = request.responseText;
return eval('(' + responseText + ')');
}
catch (e)
{
alert ("line: " + e.lineNumber + "\nmessage: " + e.message + "\nName: " + e.name);
}
}
function suggest(target_div)
{
var opt = {
// Use POST
method: 'post',
// Send this lovely data
postBody: 'Action=Edit&search_word=' + search_item + '&target=' + textarea,
// Handle successful response
onSuccess: function(t) {
var json = evalResponse (t);
data = json[1];
if (errors.errorText == undefined)
{
document.getElementById('target_div').update(data['link']);
}
},
// Handle 404
on404: function(t) {
alert('Error 404: location "' + t.statusText + '" was not found.');
},
// Handle other errors
onFailure: function(t) {
alert('Error ' + t.status + ' -- ' + t.statusText);
}
}
new Ajax.Request('handler.php', opt);
}
PHP处理程序:
$jsonArray = array();
$jsonArray[1]["link"] = '<a href="javascript:alert(\'fdg\');" onClick="return true;">link</a>';
$json = new Services_JSON();
echo $json->encode($jsonArray);