如何使用 php 在 Intel-xdk 中显示来自 MySQL 的图像。请问如何使用ajax请求在Intel xdk中显示图像? 我试图检索仅显示文本而不显示图像的信息。 我使用 php,当我请求信息时,仅显示文本。 这是我用来检索数据的代码
function showUser() {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("list").innerHTML = this.responsetext;
}
};
xmlhttp.open("GET", "http://localhost/SaltCity/index.php",true);
xmlhttp.send()
}
最佳答案
这是返回 JSON 格式的文件名的 PHP。假设您将文件名存储在 MySQL 中名为 profileimage 的字段中,并且 php 的名称为 getprofileimage.php。
$result = $conn->query("select profileimage from profiles where userid = '" . $userid . "'");
$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "[") {$outp .= ",";}
$outp .= '{"profileimage":"' . $rs["profileimage"] . '"}';
}
$outp .="]";
运行时,会返回一个像这样的 JSON 字符串
[{
"profileimage": "abcd.jpg"
}]
在 JavaScript 中,执行以下操作:
function getProfile(){
var url = "http://yourserverlocation.com" + "/getprofileimage.php";
$.ajax({
url: url,
type: 'GET',
data: JSONObject,
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (arr) {
_getProfileResult(arr);
},
error: function () {
validationMsg();
}
});
}
function _getProfileResult(arr){
profileimage = arr[0].profileimage;
//assuming that you have a <img id="imgProfilePicture> tag
$("#imgProfilePicture").attr("src","http://yourserverlocation.com/" + "/images/" + profileimage +"_s");
}
关于php - 如何使用 php 在 Intel-xdk 中显示来自 MySQL 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42527484/