2013-01-23 3 views
1

내 서버의 클라우드에있는 데이터베이스에서 데이터 테이블을 다시 가져 오려고하는 ibook이 있습니다. 내 서버에 main.html 파일을 놓고 내 웹 브라우저에서 찾아 보면 챔피언이 데이터 테이블을 반환하는 것처럼 작동하지만 Info.plist에이 HTML을 main.html 파일로 저장할 때 ibook에 표를 표시하십시오. 내가 뭘 놓치고 있니? 여기 온라인 데이터베이스에서 쿼리 된 데이터를 표시하는 방법은 무엇입니까?

책 여기

<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> 

의 페이지에 아이북의 HTML 위젯의 위젯에 내 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)를 참조하십시오. 대신 [* prepared statements *] (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 Injection에 취약합니다. SQL 문에서 값을 사용하기 전에 값을 이스케이프 처리하지 않으므로 중요한 정보와 데이터 손실이 발생할 수 있습니다. 이 사이트에서 몇 가지 예와 그 결과를 확인하십시오. http://www.unixwiz.net/techtips/sql-injection.html – thaJeztah

+0

답변을 주셔서 감사합니다 ... 둘 다 유효한 의견이지만 질문에 대한 답변은 아닙니다. 나는 다음과 같이 CORS와 관련이 있다고 결론을 내렸다. –

답변

2

연구의 몇 시간 후에 내가 내 서버에 내 PHP 파일에 다음 헤더 행을 추가하고 모든 일 빙고했다 CORS 간 리소스 공유

과 관련이있다. 이에 대한 자세한 내용은

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

는, 여기

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

간 리소스 공유 (CORS)에서 CORS의 설명입니다 브라우저에서 도메인 간 통신을 할 수있는 W3C 사양입니다 . CORS는 XmlHttpRequest 객체를 기반으로하여 개발자가 동일한 도메인 요청과 동일한 관용구로 작업 할 수 있도록합니다.

CORS의 유스 케이스는 간단합니다. 사이트 alice.com에 사이트 bob.com이 액세스하려고하는 데이터가 있다고 가정 해보십시오. 이러한 유형의 요청은 전통적으로 브라우저의 동일한 출처 정책에 따라 허용되지 않습니다. 그러나 CORS 요청을 지원함으로써 alice.com은 bob.com이 데이터에 액세스 할 수 있도록 몇 가지 특수 응답 헤더를 추가 할 수 있습니다.

이 예에서 알 수 있듯이 CORS 지원에는 서버와 클라이언트 간의 조정이 필요합니다. 다행히도 클라이언트 측 개발자 인 경우 이러한 세부 사항의 대부분을 피할 수 있습니다. 이 기사의 나머지 부분에서는 클라이언트가 교차 출처 요청을 수행하는 방법과 CORS를 지원하도록 서버를 구성하는 방법을 보여줍니다.