2017-03-01 53 views
0

如何使用php在Intel-xdk中顯示來自MySQL的圖像。請如何使用ajax請求在英特爾xdk中顯示圖像? 我試圖檢索只顯示文本而不是圖像的信息。 我使用PHP,當我請求的信息只有文本正在顯示。 這裏是我用來檢索數據如何使用php在Intel-xdk中使用php顯示MySQL中的圖像

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() 
      } 
+0

請分享一些代碼... – hering

+0

請我添加的代碼我用來檢索信息 – tyma

+0

您是否下載混合內容?如果您要將圖片作爲文本下載,則必須使用base64編碼您的響應,然後以此方式顯示。如果您正在下載html,那麼您只需將其呈現爲html並檢查所有鏈接是否已正確引用。 –

回答

0

代碼這裏的PHP在JSON返回一個文件名。這假定您將文件名存儲在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");  
} 
+0

我只存儲文件名,請我急需幫助。先謝謝你 – tyma