$.ajax({
url: "http://localhost/PhpProject1/index.php",
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(data) {
var markup = "";
$.each(data.list, function(i, elem) {
var $template = $('<div><li><img class="ui-li-icon profile"><p class="from"></p><p class="tweet"></p></li></div>');
$template.find(".from").append(elem['p_name']);
});
},
error: function(request, error) {
// This callback function will trigger on unsuccessful action
}
});
});
<?php
$mysqli_connect = mysqli_connect('localhost', 'root', '', 'nature_secret');
$query = 'select nature_secret.tbl_product.p_name from tbl_product';
$stmt = $mysqli_connect->prepare($query);
$stmt->execute();
$stmt->bind_result($productName);
$json = array();
$json['list'] = array();
$subJSON = array();
while ($stmt->fetch()) {
$subJSON['p_name'] = $productName;
array_push($json['list'], $subJSON);
}
$json_encode = json_encode($json);
echo $json_encode;
?>
我使用上面的代码运行以从localhost Server检索。为了从另一个服务器运行检索数据,我添加了以下代码片段 jsonpCallback:'jsonCallback', contentType:“application / json”, dataType:'jsonp',。当我添加零件代码不起作用时