2013-01-23 57 views
1

我有一個ibook,我試圖從我的服務器雲中的數據庫中取回數據表。如果我將main.html文件放在我的服務器上並使用我的Web瀏覽器瀏覽它,它就像一個返回數據表的champ,但是當我將這個html作爲main.html文件放入Info.plist中時,它不會在ibook中顯示錶格。我錯過了什麼?如何讓ibook顯示從在線數據庫查詢的數據

這裏是我的html文件,該文件是在iBook的HTML插件的插件的書

<html> 
<head> 
<script type="text/javascript" src="http://myserver.com/ibook_widgets/jquery-1.7.2.min.js"></script> 

<script type="text/javascript"> 
$(document).ready(function(){ 

$.ajax({ 
    type: 'post', 
    url: 'http://myserver.com/ibook_widgets/getdata.php?id=4', 
    data: 'json', 
    beforeSend: function() { 
    // before send the request, displays a "Loading..." messaj in the element where the server response will be placed 
    $('#resp').html('Loading...'); 
    }, 
    timeout: 10000,  // sets timeout for the request (10 seconds) 
    error: function(xhr, status, error) { alert('Error: '+ xhr.status+ ' - '+ error); }, 
    success: function(response) { $('#listhere').html(response); } 
}); 

}); 
</script> 
</head> 

<body> 
<div id="listhere" style="border: solid black 1px; background-color:red;">Replace this text with html table from php file</div> 
</body> 
</html> 

這裏的網頁是我的PHP文件,該文件是我的服務器上

<?php 

/* 
* Following code will list all the products 
*/ 



$dbhostname='myserver.com'; 
$dbusername='usr'; 
$dbpassword='pwd'; 
$dbname='mydatabase'; 


$con = mysql_connect($dbhostname,$dbusername,$dbpassword); 
mysql_select_db($dbname, $con); 


// check for post data 
if (isset($_POST["id"])) 
{ 
$inValue = $_POST['id']; 



$sql = 'select id, btn_txt from mytable where parent_id = '.$inValue.' order by btn_txt'; 



$result = mysql_query($sql) or die(mysql_error()); 
if (!empty($result)) 
{ 
    // check for empty result 
    if (mysql_num_rows($result) > 0) 
    { 

     $row = mysql_fetch_array($result); 


     $btntxt = $row['btn_txt']; 


      $result1 = mysql_query($sql) or die(mysql_error()); 
      // check for empty result 
      if (!empty($result1)) 
      { 
       // check for empty result 
       if (mysql_num_rows($result1) > 0) 
       { 
        // looping through all results 
        // products node 


        $tmpStr = "<table border='1'><tr><th>Tap A Row To See Details</th></tr>"; 

        // show select box input_select_4 
        while($row1 = mysql_fetch_array($result1)) 
        { 
         $tmpStr = $tmpStr . "<tr><th><a href=\"http://myserver.com/ibook_widgets/getdata.php?id=". $row1["id"] . "\" target=\"_self\">" . $row1["btn_txt"] . "</a></th></tr>"; 

        } 

        $tmpStr = $tmpStr . "</table>"; 

        echo $tmpStr; 
        // echoing JSON response 
        ///echo json_encode($tmpStr); 





      mysql_close($con); 



        // echoing JSON response 
        ////echo json_encode($response); 


       } 
      } 

     } 
    } 
} 

?> 

什麼我錯過了嗎?

+0

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – thaJeztah

+0

你的代碼對於SQL注入來說很脆弱,在你的SQL語句中使用它們之前,你沒有*轉義值,這可能導致敏感信息和數據丟失。閱讀本網站的一些例子,並可能發生什麼:http://www.unixwiz.net/techtips/sql-injection.html – thaJeztah

+0

謝謝你的答覆...這兩個都是有效的意見,但不是問題的答案我問......我最終發現了這個問題......如下所述,它與CORS有關。 –

回答

2

多小時的研究後,它與CORS跨來源資源共享

我有以下標題行添加到我的PHP文件在我的服務器和賓果它的所有工作要做。

<?php 
header("Access-Control-Allow-Origin: *"); 

有關更多細節,這裏是CORS從

http://www.html5rocks.com/en/tutorials/cors/#toc-cors-from-jquery

跨來源資源共享(CORS)的解釋是W3C規範,它允許從瀏覽器跨域通信。通過在XmlHttpRequest對象之上構建,CORS允許開發人員使用與同域請求相同的習慣用法。

CORS的用例很簡單。想象一下網站alice.com有一些網站bob.com想要訪問的數據。傳統上,這種類型的請求在瀏覽器的相同源策略下不會被允許。但是,通過支持CORS請求,alice.com可以添加一些允許bob.com訪問數據的特殊響應頭。

正如您從本例中可以看到的,CORS支持需要服務器和客戶端之間的協調。幸運的是,如果你是一位客戶端開發人員,你可以從大多數這些細節中獲得屏蔽。本文的其餘部分將介紹客戶端如何進行跨域請求以及服務器如何配置自身以支持CORS。