我尝试生成动态 JSONP 回显,但在 js ajax 调用后我的检查器工具中出现错误:
Uncaught ReferenceError: jQuery191019304498913697898_1365868235193 is not defined index.php:1
PHP:
<?php
header('Content-type: application/javascript; charset=utf-8');
$array = array();
array_push($array, array('name' => 'A', 'host' => 'A.com'));
array_push($array, array('name' => 'B', 'host' => 'B.com'));
//$jsonp = json_encode($array, JSON_FORCE_OBJECT);
$jsonp = json_encode($array);
if(isset($_GET['callback']))
{
echo $_GET['callback'].'('.$jsonp.')';
}
else
{
echo $json;
}
?>
JS:
$.ajax({
username: username,
password: password,
url: url,
dataType: 'jsonp',
crossDomain: true,
ifModified: true,
success: function (data, status, jqXHR) {
console.log(data);
$.each(data, function(index, value) {
console.log(value['name']);
});
}
});
Ajax 调用响应:
jQuery191019304498913697898_1365868235193([{"name":"A","host":"A.com"},{"name":"B","host":"B.com"}])
你有什么想法吗?我的错误在哪里?
谢谢